Multiple collisions detected on creating a scene

Hello there,

I am creating a game much like Frogger, where a player has to avoid obstacles and reach one of three “goals” at the top of the screen.  The game itself is in it’s own scene, in the previous scene there are three levels of difficulty that I pass into the scene to decide how many obstacles  to show in the game.

In the “create” method of the scene I create the player button and add the oncollision event listener to it.  This works fine, as I move the player into the “goal” at the end it triggers the collision detection (while I’m trying to solve this issue I’ve removed the obstacles, but the player collision works for these too).  

I have a back button where, at any point the user can return to the previous scene using the following two commands:

 composer.removeScene( "Scenes.boardwalk-dash-game" ) composer.gotoScene( "Scenes.boardwalk-dash", myGlobals.sceneBackTransitOptions 

In the “destroy” method of the “game” scene I am calling a function to remove the event listeners, including the collision one for the player.

 player:removeEventListener('collision', onCollision)

The problem I am seeing is that when I return to the menu screen and then go back into the game, the following happens:

First, in between the “will” and “did” show stages, I get a collision detected on a “nil” object (see below from my print to the console):

Mar 2 15:35:45.311: Showing - will Mar 2 15:35:45.338: collision detected nil Mar 2 15:35:45.569: Showing - did

Secondly, when I do then move the player into the end goal, it triggers multiple collisions:

Mar 2 15:36:36.259: collision detected nil Mar 2 15:36:38.271: collision detected goal Mar 2 15:36:38.272: collision detected goal

The “goal” item is the object I have guided the player into to complete that part of the game.  This causes all sorts of problems with trying to remove player objects, keep a counter of how many “goals” the player has achieved etc.

Interestingly the number of “collision detected” messages I get corresponds with how many times I have gone into the game screen and come out of it again, pointing very much to the fact that the at each “create” a new event listener is added, even though it should be a completely new scene being created each time.

I used some utility code to detect what events were being listened on the player - at every stage there is only one, and all the code to add and remove event listeners is being called as far as I can see it, correctly.

Also, in the “destroy” method, I’ve tried removing the player element and setting it to nil as well as just removing the event listener property - none of this makes a difference.

I thought that destroying a scene would have remove all of it’s objects and any listeners for those objects, but my code seems to be allowing the event listeners to be retained somehow.

I’ve attached my basic version of the game page (without obstacles, backgrounds etc - just enough to recreate the problem).  Hoping someone can spot what I’m doing to cause this, or (less ideal but it would do) what I can do to ensure that only the first collision is acted upon.

Thanks,

Alan

You need to stop things in the scene:hide()'s “will” phase.  Your scene might not be getting destroyed until after a new scene is on the screen, in the mean time the old scene is still running and generating collisions.

Rob

Thanks for the suggestion Rob, but I don’t believe that’s the answer - as far as I know if I used “composer.removeScene( “Scenes.boardwalk-dash-game” )” then the “hide” functionality (will or did) does not get triggered (because I’m not hiding the scene, I’m removing it altogether). 

I’ve proved that by adding some debug output to the will and did hide methods and as suspected, neither gets triggered when I go “back” out of the scene to the previous scene.

I did some further investigation and am in the middle of trying to make the event listener of the player a Runtime event listener (and then try to identify which two objects have collided, and only act if the correct two are identified).  This seems to be showing slightly better results so far, but does not answer why I keep getting multiple listeners triggered as I go in and out of the scene in what I thought was a very clean manner.

Cheers,

Alan

How can you remove a scene that’s currently on the screen?  You have to go to another scene then remove the scene after it’s off screen.

Rob

You need to stop things in the scene:hide()'s “will” phase.  Your scene might not be getting destroyed until after a new scene is on the screen, in the mean time the old scene is still running and generating collisions.

Rob

Thanks for the suggestion Rob, but I don’t believe that’s the answer - as far as I know if I used “composer.removeScene( “Scenes.boardwalk-dash-game” )” then the “hide” functionality (will or did) does not get triggered (because I’m not hiding the scene, I’m removing it altogether). 

I’ve proved that by adding some debug output to the will and did hide methods and as suspected, neither gets triggered when I go “back” out of the scene to the previous scene.

I did some further investigation and am in the middle of trying to make the event listener of the player a Runtime event listener (and then try to identify which two objects have collided, and only act if the correct two are identified).  This seems to be showing slightly better results so far, but does not answer why I keep getting multiple listeners triggered as I go in and out of the scene in what I thought was a very clean manner.

Cheers,

Alan

How can you remove a scene that’s currently on the screen?  You have to go to another scene then remove the scene after it’s off screen.

Rob