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