onCollision being called many times.

So on the first collision the console print 1 time and on the second colllision the console prints 2 times and so on… how can i fix that?

local function onCollision(event) if event.phase == "began" then if event.target.myName == "Gem" and event.other.myName == "arrow" then local removeTwo = event.other display.remove(removeTwo[hit]) print("hello") end end end arrow[aCounter].collision = onCollision arrow[aCounter]:addEventListener( "collision", arrow[aCounter]) Gem:addEventListener( "collision", onCollision )

Thanks!

It appears you’re installing a collision listener on both objects? That is redundant and not necessary. You only need to add the listener to one or the other.

Basically, a rule of thumb is that you should make collisions as efficient as possible. I’ve written pretty good descriptions of when to use “local” vs. “global” collision listeners (not to be confused with “local” and “global” in Lua variable scope) in the following guide, under the subsections of “Local Collision Handling” and “Global Collision Handling”:

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

Take care,

Brent

It appears you’re installing a collision listener on both objects? That is redundant and not necessary. You only need to add the listener to one or the other.

Basically, a rule of thumb is that you should make collisions as efficient as possible. I’ve written pretty good descriptions of when to use “local” vs. “global” collision listeners (not to be confused with “local” and “global” in Lua variable scope) in the following guide, under the subsections of “Local Collision Handling” and “Global Collision Handling”:

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

Take care,

Brent