Event.target collides only once, not multiple times

Hi guys!

When I run the onCollision function, it seems that it collides with the ‘ground’ multiple times, even though its not moving. How can I make it so once the object hits the ground, it only collides with it ‘once’? 

I would like to give a ‘score’ every time it hits the ground, but because it hits the ground multiple times, my score seems to keep going up.

here is part of the code:

    function playerCollision( self, event )     if ( event.phase == "began" ) then     --if hit bottom column, u get points     if event.target.type == "player" and event.other.type == "bottomColumn" then     print ("hit column")     onPlatform = true               else     --if hit anything else, gameOver     --composer.gotoScene( "restart" )          print ("hit ground")     end     end     end

If someone can help me out, It’d be much appreciated!

Hi @hyeonjun93,

This is caused (most probably) because the object is “bouncing” even the slightest amount, so it triggers multiple collisions.

There are several ways to work around this, including:

  1. Create a separate invisible “sensor” object which is just slightly bigger (taller) than the visible surface. Then use that for collision detection. Because it’s a sensor type object, it will not cause the object to bounce off it, and because it’s larger/taller, the object will probably not bounce above it and then fall back into it (thus causing another collision).

  2. Use a pre-collision listener to detect the first collision, which should only trigger once. All other collisions would be ignored.

Hope this helps,

Brent

Hi,

I just thought that the pre-collision listener would work just the same as the regular collision listeners because of the mini bounces that trigger the collisions in the first place. I just thought that, because of this case, the pre-collision would also work the same way, triggering multiple pre-collision triggers. But, since you are saying that it would trigger only once, I will most definitely look into it!

Thank you for your response!

It sounds just like the thing I needed!

Thank you!. 

Hi @hyeonjun93,

This is caused (most probably) because the object is “bouncing” even the slightest amount, so it triggers multiple collisions.

There are several ways to work around this, including:

  1. Create a separate invisible “sensor” object which is just slightly bigger (taller) than the visible surface. Then use that for collision detection. Because it’s a sensor type object, it will not cause the object to bounce off it, and because it’s larger/taller, the object will probably not bounce above it and then fall back into it (thus causing another collision).

  2. Use a pre-collision listener to detect the first collision, which should only trigger once. All other collisions would be ignored.

Hope this helps,

Brent

Hi,

I just thought that the pre-collision listener would work just the same as the regular collision listeners because of the mini bounces that trigger the collisions in the first place. I just thought that, because of this case, the pre-collision would also work the same way, triggering multiple pre-collision triggers. But, since you are saying that it would trigger only once, I will most definitely look into it!

Thank you for your response!

It sounds just like the thing I needed!

Thank you!.