Collisions with display groups

Hi guys,

I am trying to add some display objects into a display group and then print out a line when an object from that group collides with a dynamic object. For some reason when there is a collision I am getting an "attempt to index local ‘event’ (a nil value) error but I can’t work out why. Any ideas? I’ve tried adding a .name to the group but no luck. 

--create group monsterGroup = display.newGroup() --load display objects and add them into group local monster1=display.newSprite( monsterSheet, sequenceData) physics.addBody ( monster1, "static", {density=.1, bounce=0.1, friction=.2, radius=12}) monsterGroup:insert(monster1) local monster2=display.newSprite( monsterSheet2, sequenceData) physics.addBody ( monster2, "static", {density=.1, bounce=0.1, friction=.2, radius=12}) monsterGroup:insert(monster2) local monster3=display.newSprite( monsterSheet3, sequenceData) physics.addBody ( monster3, "static", {density=.1, bounce=0.1, friction=.2, radius=12}) monsterGroup:insert(monster3) local monster4=display.newSprite( monsterSheet3, sequenceData) physics.addBody ( monster4, "static", {density=.1, bounce=0.1, friction=.2, radius=12}) monsterGroup:insert(monster4) --add listeners monsterGroup.collision = onCollision Runtime:addEventListener ( "collision", monsterGroup ) --Collision function function onCollision (self, event) if event.phase =="began" and self.group == monsterGroup then print("collide with enemy!") end end

Hi @mark.dallamore,

At an initial glance, it looks like you’re mixing the “local type” collision handler with the “global type” handler. Please read the following guide carefully to see the difference (specifically the section titled “Collision Handling”):

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

Also, I don’t recommend that you try to detect collision on an entire display group. I think some users do this, and maybe they’ve done so successfully, but I recommend that you handle collisions on the individual monsters. If you have a lot of them, it will most likely make management of them (and their collisions) vastly easier as you add more complexity/features to your game.

Best regards,

Brent

Hi @mark.dallamore,

At an initial glance, it looks like you’re mixing the “local type” collision handler with the “global type” handler. Please read the following guide carefully to see the difference (specifically the section titled “Collision Handling”):

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

Also, I don’t recommend that you try to detect collision on an entire display group. I think some users do this, and maybe they’ve done so successfully, but I recommend that you handle collisions on the individual monsters. If you have a lot of them, it will most likely make management of them (and their collisions) vastly easier as you add more complexity/features to your game.

Best regards,

Brent