Game adds extra collision on second time playing

Im making an endless runner where a player collects coins and avoids objects that kill him… Im using the director class instead of storyboard…It runs perfectly the first time you play it, but for some reason when you click play again, every time the player collides with a coin, it ads two coins, and every time the player collides with an object that is supposed to make him lose a life, it makes him lose two lives. Usually, the app will crash on a phone on the second time you play the game…if it doesn’t and you can play a third time a similar thing happens. But this time you gain 3 coins, and you lose 3 lives. Does anyone know why this may be happening?

Thanks in advance

Hi @willgomolka99,

My guess is that you’re not cleaning up the collision listener properly when you exit. This means that there are two listeners on the object, so it queues up two collision events. Make sure that you carefully clean up all listeners (collision or otherwise) when you restart the game. Director will not clean those up for you automatically… it only handles display objects that are placed in the scene’s “localGroup” or a sub-group of that group.

Regards,

Brent

I thought about that and I dont think thats it. I have cleaned it, and whenever I clean something wrong director tells me, but nothing comes up saying that it could not clean it…

I cleaned it with:

Runtime:removeEventListener(“collision”, onCollision)

I think I’ll need to see some code in this case. :slight_smile: If you can provide it, trimmed down if possible to just the essentials of where you declare the listener, its function “onCollision”, and where you remove it, that should help.

Thanks,

Brent

I have “bricks” falling and if they hit the “player,” it should subtract one life. And I also have coins that if the player hits it should add one to coinNumber

[lua]

–CLEANING FOR DIRECTOR

function clean ( event )

   

Runtime:removeEventListener(“collision”, onCollision)

    physics.stop()

    package.loaded[physics] = nil

    physics = nil

    print(“cleaned”)

end

–COLLISION DETECTION

local function onCollision(event)

    if event.phase == “began” then

       local agro = event.object1

       local hit = event.object2

       if agro.type == “player” and hit.type == “coin” then

         coinNumber = coinNumber + 1

          coinNumber1.text = coinNumber

         transition.to(hit, { time = (1000), x= 150, y = 115, rotation = 1000, width = 0, height = 0, onComplete = display.remove(hit)} )

              

       end

       

       if agro.type == “player” and hit.type == “brick” then

      

         

        livesLeft = livesLeft - 1

           if livesLeft > -1 then

           chuchu.text = livesLeft

           end

       

       

       

       local function mySpriteListener( event )

       

       if(event.phase == “ended” ) then

       hit:removeSelf()

       hit = nil

       end

       end

       brick:addEventListener( “sprite”, mySpriteListener)

       

       

       

      

       end

    end

end

Runtime:addEventListener(“collision”, onCollision)

[/lua]

Thanks

Hi @willgomolka99,

At a quick glance, it appears that the “clean” function (where you remove the listener) is improperly scoped for the “onCollision” function. On line 7, you point to “onCollision”, but that function is scoped below, so Lua doesn’t know what you’re referring to. Moving the “clean” function below should do the trick.

Regards,

Brent

I tried that but it has made no difference

Hi @willgomolka99,

Unless the bodies involved in these collisions are multi-part (which will trigger more than one hit event unless you control it), this must be some kind of scoping issue, i.e. where you’re declaring the collision listener in one Director scene or function, but not cleaning it up in the same scope. Try to check this carefully, as it’s about the only reason you’d keep “increasing” collision counts on each restart of the game.

Regards,

Brent

Hi @willgomolka99,

My guess is that you’re not cleaning up the collision listener properly when you exit. This means that there are two listeners on the object, so it queues up two collision events. Make sure that you carefully clean up all listeners (collision or otherwise) when you restart the game. Director will not clean those up for you automatically… it only handles display objects that are placed in the scene’s “localGroup” or a sub-group of that group.

Regards,

Brent

I thought about that and I dont think thats it. I have cleaned it, and whenever I clean something wrong director tells me, but nothing comes up saying that it could not clean it…

I cleaned it with:

Runtime:removeEventListener(“collision”, onCollision)

I think I’ll need to see some code in this case. :slight_smile: If you can provide it, trimmed down if possible to just the essentials of where you declare the listener, its function “onCollision”, and where you remove it, that should help.

Thanks,

Brent

I have “bricks” falling and if they hit the “player,” it should subtract one life. And I also have coins that if the player hits it should add one to coinNumber

[lua]

–CLEANING FOR DIRECTOR

function clean ( event )

   

Runtime:removeEventListener(“collision”, onCollision)

    physics.stop()

    package.loaded[physics] = nil

    physics = nil

    print(“cleaned”)

end

–COLLISION DETECTION

local function onCollision(event)

    if event.phase == “began” then

       local agro = event.object1

       local hit = event.object2

       if agro.type == “player” and hit.type == “coin” then

         coinNumber = coinNumber + 1

          coinNumber1.text = coinNumber

         transition.to(hit, { time = (1000), x= 150, y = 115, rotation = 1000, width = 0, height = 0, onComplete = display.remove(hit)} )

              

       end

       

       if agro.type == “player” and hit.type == “brick” then

      

         

        livesLeft = livesLeft - 1

           if livesLeft > -1 then

           chuchu.text = livesLeft

           end

       

       

       

       local function mySpriteListener( event )

       

       if(event.phase == “ended” ) then

       hit:removeSelf()

       hit = nil

       end

       end

       brick:addEventListener( “sprite”, mySpriteListener)

       

       

       

      

       end

    end

end

Runtime:addEventListener(“collision”, onCollision)

[/lua]

Thanks

Hi @willgomolka99,

At a quick glance, it appears that the “clean” function (where you remove the listener) is improperly scoped for the “onCollision” function. On line 7, you point to “onCollision”, but that function is scoped below, so Lua doesn’t know what you’re referring to. Moving the “clean” function below should do the trick.

Regards,

Brent

I tried that but it has made no difference

Hi @willgomolka99,

Unless the bodies involved in these collisions are multi-part (which will trigger more than one hit event unless you control it), this must be some kind of scoping issue, i.e. where you’re declaring the collision listener in one Director scene or function, but not cleaning it up in the same scope. Try to check this carefully, as it’s about the only reason you’d keep “increasing” collision counts on each restart of the game.

Regards,

Brent