Event Listeners doubling when re-entering a scene

Hi. I have a game where the player collects coins. I have an event listener which detects collisions for these coins. I’m using director, and I remove the event listener in my clean function, but when you re-enter the game scene to play again, two collisions are being detected. I think that my event listener is not being properly removed, but I am not getting any errors. 

Here are the relevant parts of my code

local function onCollision(event) --Collision code... All of this works and is correct.. end Runtime:addEventListener("collision", onCollision) function clean ( event ) Runtime:removeEventListener("collision", onCollision) print("cleaned") end

Thanks in advance

Are you seeing the “cleaned” message in your logs?

Yes, I am.

Then I suspect you are not removing your physics bodies and they are getting re-created when you re-enter the scene and there are two physics bodies per display object.

Rob

I have a function which creates and moves the coins. And i use a timer to do this constantly. In my clean function I also cancel the timer, so I don’t know why it would be creating double the amount of coins, and I am seeing “cleaned” in my logs. Here are the relevant parts of this code. Thanks

coins = {} function updateRandomCoin(event) local check3 = math.random(450) coin = display.newImage("coin1.png", 1700, 650 - check3, true) coin.xScale = 0.6 coin.yScale = 0.6 localGroup:insert(coin) coin.type = "coin" physics.addBody(coin, "kinematic", { radius = 30, isSensor = true}) table.insert(coins, coin) transition.to( coin, { time = (4200), x= -200, rotation = 1000} ) end timer7 = timer.performWithDelay(1000, updateRandomCoin, -1) function clean ( event ) timer.cancel(timer7) print("cleaned") end

the same problem is happening with me as when i launch my app, I go to the menu screen, tap on the play button,  goes to the game part, then i go back to the menu screen and then some of the display objects from the game scene pop up in the menu screen, i’ve tried so many ways of getting rid of it, like you by puting the timers and listeners in the clean function. 

it says attempt to call method insert, when ive inserted everything

Are you seeing the “cleaned” message in your logs?

Yes, I am.

Then I suspect you are not removing your physics bodies and they are getting re-created when you re-enter the scene and there are two physics bodies per display object.

Rob

I have a function which creates and moves the coins. And i use a timer to do this constantly. In my clean function I also cancel the timer, so I don’t know why it would be creating double the amount of coins, and I am seeing “cleaned” in my logs. Here are the relevant parts of this code. Thanks

coins = {} function updateRandomCoin(event) local check3 = math.random(450) coin = display.newImage("coin1.png", 1700, 650 - check3, true) coin.xScale = 0.6 coin.yScale = 0.6 localGroup:insert(coin) coin.type = "coin" physics.addBody(coin, "kinematic", { radius = 30, isSensor = true}) table.insert(coins, coin) transition.to( coin, { time = (4200), x= -200, rotation = 1000} ) end timer7 = timer.performWithDelay(1000, updateRandomCoin, -1) function clean ( event ) timer.cancel(timer7) print("cleaned") end

the same problem is happening with me as when i launch my app, I go to the menu screen, tap on the play button,  goes to the game part, then i go back to the menu screen and then some of the display objects from the game scene pop up in the menu screen, i’ve tried so many ways of getting rid of it, like you by puting the timers and listeners in the clean function. 

it says attempt to call method insert, when ive inserted everything