Restarting scene second time doesnt work the same?

I’m starting to think this has to be a bug, because the concept seems simple to me, however I haven’t used corona alot so maybe there is something I’m missing.

The first time I run the game, it works great, objects are disappearing as expected.

Game ends:

   composer.removeScene( “game”)   --> I end the game here, everything should wipe to start over …
   composer.gotoScene( “restart”,“fade”,200)  -->  Restart screen starts up “game” again in next scene

I start the game back up easy enough

    composer.gotoScene(“game”)

However now I get crazy results.  Some objects actually count “twice” when they are hit.  Sometimes an object comes back “static” even though its hard coded as dynamic in the create and is never even changed anywhere to static in the code ever.  This only ever happens on 2nd call and later.  The key point here is the first run is always perfect, all objects disappear as expected, they never count “twice” and I never have any issues.  I thought I could easily just wipe it and start over and everything is fresh again, but thats clearly not the case here unless this is some kind of bug.  I doubt if this is a bug because everyone should be hitting this problem if so.  Wont the setup above wipe it and start over fresh?  Am I missing something to start over?   (Also on scene:show I’m not doing anything, all objects are created in the scene:create section).

Thanks

Did you cancel all your timers and remove all your eventListeners before you did removeScene?

You’re probably encountering issues w/ scope and a misunderstanding of how composer works.

Here are a number of examples you can examine to see how to use composer:

http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

If you’re using the removeScene() feature, you are still responsible for doing all of the cleanup in the scene:remove() function.

Note: I much prefer to destroy a re-used scene onExit and rebuild it onEnter instead of calling removeScene().

See example #9 specifically.

(pls excuse the typo in the folder title ‘organaization’)

Yeah there is only 4 eventlisteners, and I removed all 4 first.  I had no timers to cancel.  I even did a memory display to show that its not growing exceeding large and triggering weird results, very odd that I cant just restart a scene and get the same results as the first time in.

Thanks, I will search through this.  Although on initial look I dont see anything that looks unusual to me.   Something as simple as wiping a scene completely and starting it new again cant possibly be this hard that it never works the same :slight_smile:

Any chance the new composer is just buggy?  (I’m not using storyboards).   I even changed around all vars to be local table vars just figuring something is getting corrupted, and now the results seem even more strange when I start the scene over.  Works flawlessly the first run, but 2nd run is completely unpredictable what can happen.  The strangest thing is that sometimes it hits an object and stops (somehow becomes “static” even though its never even set that way in the code).  So it looks like its working fine after restarting the scene again, and 1 minute into it an object stops it … very odd, this should be a very simple thing to just start a scene over and have it work the same.

Well that’s how code works haha … I’d recommend you remove EVERYTHING. Like tables, object’s, timers. Anything you have in the scene remove it. Make sure your in scope and it’s actually removing.

If this doesn’t help you can add me on Skype and I can personally look at your code and help.

–SonicX278

No I think Composer is pretty solid.  Again, those problems all sound like scope issues and incorrect management/removal of objects.

Ideally you’d write your game code w/o composer, as a set of modules.  Then, when it works w/o the additional complexity of scene management, simply require it into a composer scene.  

If you start in composer, the project tends to sprawl and become error ridden.  I’ve seen some users write scene files that are thousands of lines long, which is just wrong.  A scene file should only be about 200…500 lines long at most.  

I’ve also seen users put all their game code in create(), which is really not a great practice.

Not having seen your code its hard to say what is going on here.

Note: You said 

this should be a very simple thing to just start a scene over and have it work the same.

That is ONLY true if your code is solid.  The composer scene management is simple and centralizes object management to creating scenes, removing scenes, and moving display groups (slight over-simplification, but only slight).

I suggest you put all your game code in module(s), add a start()/stop() / create()/destroy() (or equivalent) sets of functions, and test this w/o composer.  I think you’ll see you’re having the same kinds of issues.

Thanks for the feedback, it has many objects and it looks to me like I’m correctly removing everything based on everything I have looked through the past several weeks on this. I will probably scale this back at this point and start over with a smaller sample size as its now at a point where I want to know why its doing this more then actually finishing it :slight_smile:

Thanks

The Composer API is pretty solid. I’m sure there are probably bugs in obscure methods or if you are trying to use it in a non-standard way.

The first thing I noticed is you are apparently trying to remove your “game” scene while’ you’re still in the “game” scene. My simple way of doing this is call composer.removeScene(“sceneName”) **BEFORE** you go to it, not while you’re still in it.

I highly recommend that you create things in scene:create(), start things in scene:show(); stop them in scene:hide(). If you’re using composer.removeScene() any display object you put into the scene’s view group will be cleaned up for you. scene:destroy() only gets called when composer.removeScene() is called and its an opportunity to dump audio that you may have loaded in scene:create() or other large data structures that are not managed display objects.

Rob

Thanks for the feedback, I’m going to start this over from scratch to get the structure right, and hopefully that will fix this issue.

I have problem with my game. This is my code for my gamefile. MY flow is main lua>startgame>>game>>restart>>game>>restart and so on…

local composer=require("composer") local physics=require("physics") local widget=require("widget") local scene=composer.newScene() physics.setDrawMode("normal") --erased system activate check later system.activate("multitouch") \_H=display.contentHeight \_W=display.contentWidth &nbsp;trumps=0 &nbsp;numTrumps=100 &nbsp;startTime=20 &nbsp;totalTime=20 &nbsp;timeLeft=true &nbsp;playerReady=false Random=math.random local function Restart(event) if "began"==event.phase then&nbsp; print("hello") end if "moved"==event.phase then end if "ended"==event.phase then composer.gotoScene("restarttest") print("hello") end end local function gameOver(condition) if (condition=="winner") then screenText.text="Adios Trump!" elseif (condition=="notbad") then screenText.text="Barely stopped Trump" elseif (condition=="loser") then screenText.text="Ready for That Wall!" end end local function removeTrumps(obj) obj:removeSelf() trumps=trumps-1 if (timeLeft~=false) then&nbsp; if(trumps==0) then timer.cancel(gameTimer) gameOver("winner") elseif(trumps\<=40) then gameOver("notbad") elseif(trumps\>=31) then gameOver("loser") end end end local function countDown(event) if (startTime==totalTime) then playerReady=true screenText.text="Hurry!" end startTime=startTime-1 timeText.text="Time: "..startTime if (startTime==0) then timeLeft=false end end local function startGame() myTrump=display.newImageRect("tp.png",25,25) myTrump.x=Random(50,\_W-50) myTrump.y=(\_H+10) physics.addBody(myTrump,"dynamic",{density=.1,friction=0,bounce=.9,radius=10}) &nbsp; &nbsp;function myTrump:touch(event) if(timeLeft~=false) then if (playerReady==true) then if(event.phase=="ended") then removeTrumps(self) end end end end myTrump:addEventListener("touch",myTrump) trumps=trumps+1 if(trumps==numTrumps) then gameTimer=timer.performWithDelay(1000,countDown,totalTime) else playerReady=false end end &nbsp; local gameTimer=timer.performWithDelay(20,startGame,numTrumps) function scene:create(event) local screenGroup=display.newGroup() &nbsp; background=display.newImage(screenGroup,"tflag.png") &nbsp; but1=widget.newButton{screenGroup,label="restart",onEvent=Restart} but1.x=\_W/2+150 but1.y=\_H-10 &nbsp;screenText=display.newText(screenGroup,"...Waiting for Trump...",0,0,native.systemFont,16\*2) screenText.xScale=.5 screenText.yScale=.5 screenText.anchorX=(0) screenText.anchorY=(0) screenText.x=\_W/2-210 screenText.y=\_H-20 &nbsp; timeText=display.newText(screenGroup,"Time:"..startTime,0,0,native.systemFont,16\*2) timeText.xScale=.5 timeText.yScale=.5 timeText.anchorY=0 timeText.anchorX=0 timeText.x=\_W/2 timeText.y=\_H-20 &nbsp;leftWall=display.newRect(screenGroup,483,50,1,1000) &nbsp; &nbsp; &nbsp; rightWall=display.newRect(screenGroup,0,0,1,1000) &nbsp; &nbsp; &nbsp; ceiling=display.newRect(screenGroup,150,0,1000,1) &nbsp;&nbsp; end function scene:show(event) local screenGroup=display.newGroup() if event.phase=="will" then &nbsp;physics.start() &nbsp;physics.setGravity(0,-5) physics.addBody(leftWall,"static",{bounce=.1}) physics.addBody(rightWall,"static",{bounce=.1}) physics.addBody(ceiling,"static",{bounce=.1}) &nbsp; end &nbsp;if event.phase=="did" then&nbsp; &nbsp;end&nbsp; end function scene:hide(event) local screenGroup=display.newGroup() if event.phase=="will" then end if event.phase=="did" then Runtime:removeEventListener("touch",myTrump) end end function scene:destroy(event) local screenGroup=display.newGroup() end scene:addEventListener("create",scene) scene:addEventListener("show",scene) scene:addEventListener("hide",scene) scene:addEventListener("destroy",scene) return scene

Here is my restart files code:

local composer=require("composer") local scene=composer.newScene() --background function scene:create(event) local screenGroup=display.newGroup() background=display.newImage(screenGroup,"tflag.png") end function start(event) if event.phase=="began" then composer.removeScene("game1") &nbsp; &nbsp;&nbsp; &nbsp; end &nbsp; if event.phase=="ended" then &nbsp; composer.gotoScene("game1") end end function scene:show(event) local screenGroup=display.newGroup() background:addEventListener("touch",start) end function scene:hide(event) local screenGroup=display.newGroup() if event.phase=="will" then background:removeEventListener("touch",start) end if event.phase=="did" then&nbsp; end &nbsp; &nbsp; end function scene:destroy(event) local screenGroup=self.view end scene:addEventListener("create",scene) scene:addEventListener("show",scene) scene:addEventListener("hide",scene) scene:addEventListener("destroy",scene) return scene

My problems are:

After the intial run through i click on the restart button and i am taken to the restart scene and from there when i go back to the game scene the timer that starts at 20 goes past 0 (onto negative numbers) each time i try to click the pictures my game just restarts and my game no longer works.

Also in the initial run through my pictures(the trumps that you click and when they are clicked they are gotten rid of) go to the very top but when i go back to the game scene after the restart scene they go midway so it is like the ceiling fell down a bit!

Can i please have help and can you tell me what i need to fix or just copy my code and edit the mistakes and then post it as the answer.  Thank you!

Please use the blue <> button in the formatting bar and paste your code into the box that pops up. This will make your code easier to read. Also only ask your question once. I’ve removed your other two posts.

Thanks

Rob

Sorry but I didn’t know if someone would answer me or not. Can you help me?

Did you cancel all your timers and remove all your eventListeners before you did removeScene?

You’re probably encountering issues w/ scope and a misunderstanding of how composer works.

Here are a number of examples you can examine to see how to use composer:

http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

If you’re using the removeScene() feature, you are still responsible for doing all of the cleanup in the scene:remove() function.

Note: I much prefer to destroy a re-used scene onExit and rebuild it onEnter instead of calling removeScene().

See example #9 specifically.

(pls excuse the typo in the folder title ‘organaization’)

Yeah there is only 4 eventlisteners, and I removed all 4 first.  I had no timers to cancel.  I even did a memory display to show that its not growing exceeding large and triggering weird results, very odd that I cant just restart a scene and get the same results as the first time in.

Thanks, I will search through this.  Although on initial look I dont see anything that looks unusual to me.   Something as simple as wiping a scene completely and starting it new again cant possibly be this hard that it never works the same :slight_smile:

Any chance the new composer is just buggy?  (I’m not using storyboards).   I even changed around all vars to be local table vars just figuring something is getting corrupted, and now the results seem even more strange when I start the scene over.  Works flawlessly the first run, but 2nd run is completely unpredictable what can happen.  The strangest thing is that sometimes it hits an object and stops (somehow becomes “static” even though its never even set that way in the code).  So it looks like its working fine after restarting the scene again, and 1 minute into it an object stops it … very odd, this should be a very simple thing to just start a scene over and have it work the same.

Well that’s how code works haha … I’d recommend you remove EVERYTHING. Like tables, object’s, timers. Anything you have in the scene remove it. Make sure your in scope and it’s actually removing.

If this doesn’t help you can add me on Skype and I can personally look at your code and help.

–SonicX278