Help with collision removing object

I need help so when the “onCollision” even happens, the object removes itself. So when you hit the restart button and it goes back to the game scene, everything restarts and there are no leftover objects.

Code:

local function spawnArrowsRight() arrowFour[aCounterFour] = display.newImageRect( "Spear.png", 20, 73 ) arrowFour[aCounterFour].x = display.contentWidth \* 1.2 arrowFour[aCounterFour].y = display.contentCenterY arrowFour[aCounterFour].rotation = -90 physics.addBody( arrowFour[aCounterFour], "dynamic", { bounce = 0 }) arrowFour[aCounterFour].gravityScale = 0 arrowFour[aCounterFour].myName = "arrowFour" arrowFour[aCounterFour].value = aCounterFour arrowFour[aCounterFour]:setLinearVelocity( -150, 0 ) sceneGroup:insert(arrowFour[aCounterFour]) local function removeArrow4(event) if event.phase == "began" then local removeFour = event.target display.remove(removeFour) end end function onCollisionFour(event) if event.phase == "began" then if event.target.myName == "Gem" and event.other.myName == "arrowFour" then composer.gotoScene("GameOver") print("Hahahaha") end end end arrowFour[aCounterFour].collision = onCollisionFour arrowFour[aCounterFour]:addEventListener( "collision", arrowFour[aCounterFour]) Gem:addEventListener( "collision", onCollisionFour ) arrowFour[aCounterFour]:addEventListener( "touch", removeArrow4 ) aCounterFour = aCounterFour + 1 end timer4 = timer.performWithDelay( math.random(1000,2000), spawnArrowsRight, -1 ) end end

So when the arrow collides, it does go to the GameOver scene. So everything works. But the only thing is, is that when you restart the arrows are still spawning. But you can’t see that unless you are in hybrid mode. (The arrows keep spawning, but we didn’t make it so the game restarts yet)

Hi @Spid3rtech,

The arrows are being spawned on the repeating timer, so unless you cancel (or pause) the timer, they will continue to spawn, no matter what other scene you’re in or what else the app is doing.

Brent

Hey Brent. Thanks. I added a timer.cancel(timer4) to the onCollision, and it works. The only thing is, is that when you hit restart, that one certain Arrow still stays there(The one that collided).

Also, I realized I needed a Global function to turn off all of the timers(timer1, timer2, timer3, etc.) on collision. How would I add a global function? It would help more if you gave me the code to the global function.

  1. If the arrow (that collided) is part of your scene group, it should be removed when you leave the scene. If that’s not happening, it’s some issue in how you’ve coded it.

  2. You should basically never create global functions or variables… yes, they are technically legal in Lua, but they are bad practice and you should make everything local if possible.

  3. For managing multiple timers, you may benefit from this post:

https://forums.coronalabs.com/topic/54775-removing-timers-on-scene-change-help-needed-please/

Best regards,

Brent

Hi @Spid3rtech,

The arrows are being spawned on the repeating timer, so unless you cancel (or pause) the timer, they will continue to spawn, no matter what other scene you’re in or what else the app is doing.

Brent

Hey Brent. Thanks. I added a timer.cancel(timer4) to the onCollision, and it works. The only thing is, is that when you hit restart, that one certain Arrow still stays there(The one that collided).

Also, I realized I needed a Global function to turn off all of the timers(timer1, timer2, timer3, etc.) on collision. How would I add a global function? It would help more if you gave me the code to the global function.

  1. If the arrow (that collided) is part of your scene group, it should be removed when you leave the scene. If that’s not happening, it’s some issue in how you’ve coded it.

  2. You should basically never create global functions or variables… yes, they are technically legal in Lua, but they are bad practice and you should make everything local if possible.

  3. For managing multiple timers, you may benefit from this post:

https://forums.coronalabs.com/topic/54775-removing-timers-on-scene-change-help-needed-please/

Best regards,

Brent