brdybrbnk
brdybrbnk

Reputation: 13

Passing data in functions, doing it wrong?

I'm using Corona SDK to create an Android/iOS app. And I'm trying to pass two different parameters in a function. The function is called like this:

function onCollision( self, event )

The problem is, when the function is called, it returns this error: attempt to index local "event" a nil value. I know why, I think it's because of the comma. But I've read documentation and that's how you're supposed to do it, any help?

Upvotes: 0

Views: 102

Answers (2)

Oliver
Oliver

Reputation: 29621

If you give a table object and reference your function as part of the table it should work:

local object = display.newImage( "object.png" )
physics.addBody( object , { ... } )

local function onCollision( self, event )
    ...
end

object.collision = onCollision
object:addEventListener( "collision", object)

Upvotes: 1

Teddy Engel
Teddy Engel

Reputation: 1006

Your function should be function onCollision(event), self isn't needed.

If you actually want to pass another parameter to this function, you can do it using a closure like that:

local myParam = 1
Runtime:addEventListener ( "collision", function(event) 
                                         return onCollision(event, myParam) 
                                        end )

Upvotes: 0

Related Questions