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)