How to filter Collisions?

Is there a way to get which 2 objects are colliding with object:addEventListener(“collision”)? I’m trying to make it so a projectile will remove an object it hits along with itself on collision.

yes, using documentation is first skill programmer should learn :slight_smile:

http://docs.coronalabs.com/api/event/collision/index.html

I don’t understand most of it :/. But I tried doing this and it worked for when a projectile collides with Enemy and it only removes Enemy if projectile collides with Enemy

  1.     local function onLocalCollision(event)
  2.         if projectile and Enemy and EnemyAlive == 1 then
  3.             Enemy:removeSelf(Enemy)
  4.             projectile:removeSelf()
  5.             EnemyAlive = 0
  6.             print (event.name1)
  7.         end
  8.     end
  9.     Enemy:addEventListener(“collision”, onLocalCollision)

But when I tried to do

    

  1. local function onLocalCollision2(event)
  2.         if Hero and Enemy then
  3.             Hero:removeSelf(Hero)
  4.             Enemy:removeSelf(Enemy)
  5.             EnemyAlive = 0
  6.         end
  7. end
  8. Hero:addEventListener(“collision”, onLocalCollision2)

the Hero would remove itself on contact with the floor along with the Enemy

You don’t check what collides with what. In second case you just remove hero on first collision (hero also collides with floor so no supprise that he is removed when it happens). Look into docs once again. Event has .object2 properties telling you what collided with object.

Going back to projectile/enemy.
When collision happens you must check what collided with enemy. We want to ignore collision if it is with floor but handle it when it’s projectile. So we somehow must mark that projectile is projectile.
If you have for example object called ‘projectile’ then add to it property .isProjectile.

So in collision handler for enemy you check: event.object2.isProjectile and if it’s true then you can handle it further.

yes, using documentation is first skill programmer should learn :slight_smile:

http://docs.coronalabs.com/api/event/collision/index.html

I don’t understand most of it :/. But I tried doing this and it worked for when a projectile collides with Enemy and it only removes Enemy if projectile collides with Enemy

  1.     local function onLocalCollision(event)
  2.         if projectile and Enemy and EnemyAlive == 1 then
  3.             Enemy:removeSelf(Enemy)
  4.             projectile:removeSelf()
  5.             EnemyAlive = 0
  6.             print (event.name1)
  7.         end
  8.     end
  9.     Enemy:addEventListener(“collision”, onLocalCollision)

But when I tried to do

    

  1. local function onLocalCollision2(event)
  2.         if Hero and Enemy then
  3.             Hero:removeSelf(Hero)
  4.             Enemy:removeSelf(Enemy)
  5.             EnemyAlive = 0
  6.         end
  7. end
  8. Hero:addEventListener(“collision”, onLocalCollision2)

the Hero would remove itself on contact with the floor along with the Enemy

You don’t check what collides with what. In second case you just remove hero on first collision (hero also collides with floor so no supprise that he is removed when it happens). Look into docs once again. Event has .object2 properties telling you what collided with object.

Going back to projectile/enemy.
When collision happens you must check what collided with enemy. We want to ignore collision if it is with floor but handle it when it’s projectile. So we somehow must mark that projectile is projectile.
If you have for example object called ‘projectile’ then add to it property .isProjectile.

So in collision handler for enemy you check: event.object2.isProjectile and if it’s true then you can handle it further.