I’m pretty new to Corona and just implemented storyboards in a game app I’m writing. The storyboards work, or I can make them work, but I’d really like to understand what I’m doing. Specifically as related to parameters of event callbacks.
In one of my scenes I have a couple of buttons. To handle when the user presses the button I add this:
function scene:createScene( event )
local hud = self.view
self.leftButton = widget.newButton{
id = "leftButton",
default = "images/leftButton.png",
over = "images/leftButtonPress.png",
width = 283,
height = 124,
onEvent = onButtonEvent
}
hud:insert( self.leftButton )
... more stuff
and this calls a function in my scene file defined like this:
local function onButtonEvent( self, event )
local btn = self.target
if self.phase == "press" then
if btn.id == "leftButton" then
ship:rotateLeft()
... more code
Notice I reference self to get the target, not event, like I would in a non-scene file. This works, but I don’t understand why I can’t just use event. If I do I get an error “Attempt to index local ‘event’”
Now I need to implement collision detection in a scene, so I thought I’d make the collision callback part of the scene. I defined things like this:
function scene:onCollision( self, event )
print( self.object1)
And created like this:
function scene:enterScene( event )
Runtime:addEventListener( "collision", self.onCollision )
This doesn’t work and I get the error “attempt to index local ‘self’ (a nil value)” In the callback.
I can create the collision call back like the button one, but that means I can’t access scene specific data via the self variable.
I know this is a pretty big question, but any help would be appreciated.
Ron [import]uid: 186688 topic_id: 33568 reply_id: 333568[/import]