Hey guys how do I change scenes with composer api. I have tried alot but nothing seems to work.I am trying to make a “you lose scene”.
if playerhlth == 0 then composer.removeScene( "level1", true ) composer.gotoScene("lose") end
Thanks, Tyler
Hey guys how do I change scenes with composer api. I have tried alot but nothing seems to work.I am trying to make a “you lose scene”.
if playerhlth == 0 then composer.removeScene( "level1", true ) composer.gotoScene("lose") end
Thanks, Tyler
How I do it in my game is when my character dies, I call the lose class as an overlay instead of gotoScene. Then when I press retry it goes to a transition scene with a timer.performWithDelay, which transitions to going back to the level scene.
So an example would be:
if playerHlth == 0 then -- pause or cancel all your events and timers here composer.showOverlay("lose") end
Once the lose scene is on screen, if you have a retry button call the transition scene when you press it
-- Outside the scene:create local function onButtonRelease(event) local go = event.target.id if go == "quit" then composer.hideOverlay() composer.gotoScene("mainMenu") elseif go == "levelSel" then composer.hideOverlay() composer.gotoScene("levelSel") elseif go == "retry" then composer.hideOverlay() --composer.removeScene("level1") composer.gotoScene("retry") end end -- This is inside the scene local retryBtn = widget.newButton({width = 200, height = 77, defaultFile = "gameOver/restartBtn.png", id = "retry", onRelease = onButtonRelease}) retryBtn.x = 600 retryBtn.y = 110 sceneGroup:insert(retryBtn)
You’ll notice this is where I remove the level1 scene instead of inside the level class. Once I press the retry button, the class is hidden, the level class is removed, and it takes me to my transition scene where it will reload the level.
-- Inside the scene:show phase == will local getPrev = composer.getPrevious() if getPrev ~= nil then composer.removeScene(getPrev) end -- Inside the scene:show phase == did local function goTo() composer.gotoScene("level1") end timer.performWithDelay(500, goTo)
The getPrev scene function removes the game over scene once this retry scene has been loaded in.
Hope this helps!
You might want to read this week’s tutorial.
https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/
Rob
How I do it in my game is when my character dies, I call the lose class as an overlay instead of gotoScene. Then when I press retry it goes to a transition scene with a timer.performWithDelay, which transitions to going back to the level scene.
So an example would be:
if playerHlth == 0 then -- pause or cancel all your events and timers here composer.showOverlay("lose") end
Once the lose scene is on screen, if you have a retry button call the transition scene when you press it
-- Outside the scene:create local function onButtonRelease(event) local go = event.target.id if go == "quit" then composer.hideOverlay() composer.gotoScene("mainMenu") elseif go == "levelSel" then composer.hideOverlay() composer.gotoScene("levelSel") elseif go == "retry" then composer.hideOverlay() --composer.removeScene("level1") composer.gotoScene("retry") end end -- This is inside the scene local retryBtn = widget.newButton({width = 200, height = 77, defaultFile = "gameOver/restartBtn.png", id = "retry", onRelease = onButtonRelease}) retryBtn.x = 600 retryBtn.y = 110 sceneGroup:insert(retryBtn)
You’ll notice this is where I remove the level1 scene instead of inside the level class. Once I press the retry button, the class is hidden, the level class is removed, and it takes me to my transition scene where it will reload the level.
-- Inside the scene:show phase == will local getPrev = composer.getPrevious() if getPrev ~= nil then composer.removeScene(getPrev) end -- Inside the scene:show phase == did local function goTo() composer.gotoScene("level1") end timer.performWithDelay(500, goTo)
The getPrev scene function removes the game over scene once this retry scene has been loaded in.
Hope this helps!
You might want to read this week’s tutorial.
https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/
Rob