So I thought I was getting to really understand variable scoping and the use of forward declarations to prevent problems when, for example, a function is defined somewhere after it is called.
However, I tried to setup a button that calls a function positioned right after its definition (but with forward declaration) and it didn’t work. Moving the function above the button declaration works. But can someone explain why the forward declaration of the function doesn’t work in this case? Here’s my sample code:
I should add that this code occurs within a createScene function.
local startGame
local startBtn = widget.newButton{
defaultFile = “images/goBtn.png”,
overFile = “images/goBtn_over.png”,
width = 60,
height = 60,
left = 30,
top = 120,
onEvent = startGame
}
function startGame(e)
if ( e.phase == “ended” ) then
print(“start clicked”)
storyboard.gotoScene( “game”, “crossFade”, 10 )
end
return true
end