I found some problems with your code demo, “Orientation”. First, on your download page, most of the buttons either point to the wrong zip file or don’t work at all:
http://developer.anscamobile.com/sample-code/interface
Looks like someone tried copy/pasting but didn’t finish the process (or test).
On your original Orientation demo, you have the following code:
--
-- Abstract: Orientation sample app
--
-- Version: 1.0
--
-- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
-- Copyright (C) 2010 ANSCA Inc. All Rights Reserved.
-- This demonstrates how to handle orientation changes manually, by rotating elements within
-- Corona. Note that the Corona stage remains fixed in place, and only the text rotates in this case.
--
-- The advantage of this method is that you have full control over how to handle the change, as in
-- the animation shown here. The disadvantage is that native UI elements will not rotate.
--
-- Alternatively, you can use the device's automatic orientation changes to rotate the entire stage,
-- which will also rotate native UI elements. See the "NativeOrientation" sample code for more.
local label = display.newText( "portrait", 0, 0, nil, 30 )
label:setTextColor( 255,255,255 )
label.x = display.contentWidth/2
label.y = display.contentHeight/2
local function onOrientationChange( event )
-- change text to reflect current orientation
label.text = event.type
local direction = event.type
-- rotate text so it remains upright
local newAngle = label.rotation - event.delta
transition.to( label, { time=150, rotation=newAngle } )
end
Runtime:addEventListener( "orientation", onOrientationChange )
But there are a couple of problems:
If you rotate really fast (easy to do in the simulator), you might test the current label.rotation before it finishes its transition to it’s final resting place for the new orientation. This results in rotations that are no longer on 90° boundaries.
Another is if you’re starting the device in face up or face down orientation.
And another is if the device is at a non-90° angle when you start it up… it’s possible to end up with a diagonal orientation.
Here’s my version of the code which seems to handle those problems, but which shows a couple more problems:
--
-- Abstract: Orientation sample app
--
-- Version: 1.1
--
-- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
-- Copyright (C) 2010 ANSCA Inc. All Rights Reserved.
-- This demonstrates how to handle orientation changes manually, by rotating elements within
-- Corona. Note that the Corona stage remains fixed in place, and only the text rotates in this case.
--
-- The advantage of this method is that you have full control over how to handle the change, as in
-- the animation shown here. The disadvantage is that native UI elements will not rotate.
--
-- Alternatively, you can use the device's automatic orientation changes to rotate the entire stage,
-- which will also rotate native UI elements. See the "NativeOrientation" sample code for more.
--
-- Changes 11/2/10 - Make sure initial angle value is always on a 90° boundary,
-- position the label in the correct orientation regardless of the position of the device on startup,
-- and don't test the label.rotation while it might be in the middle of a rotation transition
local startDirection = system.orientation
local label = display.newText( startDirection, 0, 0, nil, 30 )
label:setTextColor( 255,255,255 )
label.x = display.contentWidth/2
label.y = display.contentHeight/2
local currentRotation = 0 -- assume portrait to start. Also use this if faceUp/Down
if (startDirection == "landscapeRight") then
currentRotation = 90
elseif (startDirection == "portraitUpsideDown") then
currentRotation = 180
elseif (startDirection == "landscapeLeft") then
currentRotation = 270
end
label.rotation = currentRotation
local function onOrientationChange( event )
-- change text to reflect current orientation
label.text = event.type
local direction = event.type
-- rotate text so it remains upright
currentRotation = currentRotation - event.delta
transition.to( label, { time=150, rotation=currentRotation } )
end
Runtime:addEventListener( "orientation", onOrientationChange )
Problems I’m now seeing are that even though I’m trying to check initial orientation, when I load this on an iPad while holding it in anything other than Portrait, I’ll sometimes see the word “portrait” flash on the screen before displaying the correct orientation name, and other times I’ll see a very slight rotation into position. If I remove the event listener and test, the proper orientation name does appear right away.
So I’m thinking the event listener is maybe triggering once before it should be when the program first starts… as if it had a cached orientation event it thought it had to deal with.
When I run it on an iPhone (3G), there is always an animated transition to the starting position… it never displays the image in the correct orientation at start unless that happens to be Portrait.
Any ideas on this?
[import]uid: 9905 topic_id: 3324 reply_id: 303324[/import]