Reputation: 162
I'm working from a book and learning about orientation. I'm very confused about the code event.type.
Here's the code from the book:
portrait = display.newText ("Portrait", display.contentWidth / 2,display.contentHeight / 2, nil,20)
portrait: setFillColor ( 1,1,1 )
portrait.alpha = 1
landscape = display.newText ("Landscape", display.contentWidth/ 2, display.contentHeight /2, nil, 20)
landscape: setFillColor (1,1,1)
landscape.alpha = 0
local function onOrientationChange (event)
if (event.type == 'landscapeRight' or event.type == 'landscapeLeft') then
local newAngle = landscape.rotation - event.delta
transition.to (landscape, {time = 1500, rotation = newAngle})
transition.to (portrait, {rotation = newAngle})
portrait.alpha = 0
landscape.alpha = 1
else
local newAngle = portrait.rotation - event.delta
transition.to (portrait, {time = 150, rotation = newAngle})
transition.to (landscape, {rotation = newAngle})
portrait.alpha = 1
landscape.alpha = 0
end
end
Seems like the whole orientation change function works around event.type. I don't understand what it is, and I don't understand what it's equal to (==
). Further when I change the string(in this case 'landscapeRight' and 'landscapeLeft') that it's equal too. It will take any value and still function okay. I'm completely confused on how it works, please explain event.type.
Upvotes: 1
Views: 154
Reputation: 29483
I hope you accept MBlanc's answer, I'm just going to expand here: the orientation event.type is a value that is one of several strings, as indicated on that link in MBlanc's post. Event.type will never be anything other than those strings. So what you are doing by changing the comparison to strings that can never match event.type is ending up in the "else" branch all the time, as though your device was never oriented in landscape:
local function onOrientationChange (event)
if (event.type == 'blabla1' or event.type == 'blabla2') then
...do stuff -- but will never get run because event.type can never have those values
else -- so your program always ends up here:
...do other stuff...
end
end
This will make it appear as though program is running fine except that when your device really is in orientation landscapeRight or left the program will execute the "else" block instead of the block it should have executed (first block).
Upvotes: 1
Reputation: 1783
It is a common Lua idiom to use strings such 'landscapeRight'
as enum literals.
For orientation
events, event.type
holds the new device orientation.
In the code snippet you provided, you don't seem to be calling or otherwise making any reference to onOrientationChange
after defining it. You should attach it to the Runtime
object using
Runtime:addEventListener('orientation', onOrientationChange)
Upvotes: 2