Two Ways to Call Collision Listener?

Most of the examples (I’ve seen from CL) show this method of calling a collision listener:

[lua]

local function myCollision(self, event)

    …

end

player.collision = myCollision

player:addEventListener(“collision”, player)

[/lua]

However, in my tests the following works, is more straight-forward, and uses less code:

[lua]

local function myCollision(event)

    …

end

player:addEventListener(“collision”, myCollision)

[/lua]

Is there some advantage to the first method that I’m not seeing? 

 Jay

the second method is most useful if you only have one body that you need to listen for collisions.

The first method is far better if you are listening for collisions on more than one body.  For instance, you might have some baddies you are trying to kill.

By using the first method you can add the same collision listener to every baddie that you add and manipulate them using the self property.  For example every time you shoot a baddie you can do :

display.remove(self)

without affecting all the other baddies.

Yeah, but even in the second method you can use event.target and event.other to see which two objects are colliding, so there’s still no benefit (that I see) to the first method.

 Jay

the second method is most useful if you only have one body that you need to listen for collisions.

The first method is far better if you are listening for collisions on more than one body.  For instance, you might have some baddies you are trying to kill.

By using the first method you can add the same collision listener to every baddie that you add and manipulate them using the self property.  For example every time you shoot a baddie you can do :

display.remove(self)

without affecting all the other baddies.

Yeah, but even in the second method you can use event.target and event.other to see which two objects are colliding, so there’s still no benefit (that I see) to the first method.

 Jay