Passing parameters/data to functions?

Hey, I’m trying to pass two different parameters in a function. The function is called like this:

[lua]

function onCollision( self, event )

[/lua]

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?

You don’t pass parameters to event handling functions.  The event handling system does.  There is a technique called closures that can let you work around it.

I think we need to see a little more code to see what you’re doing and make a good recommendation.

Rob

Here’s the code for it:

[lua]

function onCollision(self, event)

   if event.phase == “began” then

      if self.name == “airplane” and event.other.name == “enemy” then

             --removes stuff

             destroy()

       elseif self.name == “airplane” and event.other.name == “rocket” then

             destroy()

       end

   end

end

[/lua]

The code should be checking what it’s colliding with. If it collides with a rocket or enemy, it should destroy it, but if it collides with other stuff, other stuff will happen. I built this based off a tutorial I remember seeing when I started.

Can you also post where you are calling this function from?

Thanks

Rob

This sounds like you’re setting up your listener as the “global” listener type instead of the “local” type. We’d need to see where you’re setting this up.

Thanks,

Brent

I’m calling the function when the player character (named airplane) is added to the stage. I used this function to add it:

[lua]

function adPlane()

   airplane = display.newImageRect( “airplane.png”, 88, 28 )

   airplane.x, airplane.y = 60, display.contentHeight/2

   physics.addBody( airplane, “dynamic”, {filter=planeFilter} )

   airplane.gravityScale = 0

   airplane:addEventListener( “collision”, onCollision )

end

[/lua]

You’re missing the .collision property which additionally refers to the listener function:

[lua]

airplane.collision = onCollision  --THIS IS MISSING!

airplane:addEventListener( “collision”, onCollision )

[/lua]

Brent

Also, your addEventListener line should make reference to the object again, not the listener.

[lua]

airplane:addEventListener( “collision”, airplane )

[/lua]

The collision handling examples here outline the difference:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html#handling

Brent

You don’t pass parameters to event handling functions.  The event handling system does.  There is a technique called closures that can let you work around it.

I think we need to see a little more code to see what you’re doing and make a good recommendation.

Rob

Here’s the code for it:

[lua]

function onCollision(self, event)

   if event.phase == “began” then

      if self.name == “airplane” and event.other.name == “enemy” then

             --removes stuff

             destroy()

       elseif self.name == “airplane” and event.other.name == “rocket” then

             destroy()

       end

   end

end

[/lua]

The code should be checking what it’s colliding with. If it collides with a rocket or enemy, it should destroy it, but if it collides with other stuff, other stuff will happen. I built this based off a tutorial I remember seeing when I started.

Can you also post where you are calling this function from?

Thanks

Rob

This sounds like you’re setting up your listener as the “global” listener type instead of the “local” type. We’d need to see where you’re setting this up.

Thanks,

Brent

I’m calling the function when the player character (named airplane) is added to the stage. I used this function to add it:

[lua]

function adPlane()

   airplane = display.newImageRect( “airplane.png”, 88, 28 )

   airplane.x, airplane.y = 60, display.contentHeight/2

   physics.addBody( airplane, “dynamic”, {filter=planeFilter} )

   airplane.gravityScale = 0

   airplane:addEventListener( “collision”, onCollision )

end

[/lua]

You’re missing the .collision property which additionally refers to the listener function:

[lua]

airplane.collision = onCollision  --THIS IS MISSING!

airplane:addEventListener( “collision”, onCollision )

[/lua]

Brent

Also, your addEventListener line should make reference to the object again, not the listener.

[lua]

airplane:addEventListener( “collision”, airplane )

[/lua]

The collision handling examples here outline the difference:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html#handling

Brent