Best Way To Restart Game In Storyboard?

Hello,

I have a game in a scene that works great. After they play once I show a button to play again. I am trying to reload the scene but it is not. I also try to go to another scene and it doesn’t. I added a print statement in the button event so I know it is firing the listener when the button is clicked. I tried adding code to purge the scene when the button is clicked and then reload. And even a sceneGoto but it doesn’t do anything. No errors, just not working. How can I get to another scene? I’m stuck after one level.

Thanks,

Warren

local function onbtnPlayAgainEvent( event ) local phase = event.phase if "ended" == phase then print( "abc" ) storyboard.reloadScene() end end

Here are a couple of things.

1.  You cannot purge the scene you are currently in.

2.  Are you saying storyboard.gotoScene(“somescene”) doesn’t work?

I tried different variations of the code and also without the purge in there. What I am trying to do is reload the scene to play again. I tried the reload and also the gotoScene. The print action worked but nothing with the scene. So I know the listener is working. I have this written for an android although it mat work for apple but have not yet tried it. I can let you try and see youself with the game but I don’t want to post a link public for everyone to see. I have been trying to a couple weeks to get this to work. It is a basic throwing game like angry birds but another theme with one try. It’s a mini game to go with an app. After all of the objects stop moving based on their velocity I show a button to play again. I also need to add a button to return to the app so I need to use the gotoScene method also.

Thanks for your help!

Warren

Does anyone know why the storyboard.reloadScene() not work here? I also added a new scene and tried the storyboard.loadScene(“scenehere”) and the gotoScene and neither worked. Why would it be disabled or not working in the button listener? I am really stuck here…

Thanks,

Warren

My solution is usually to have an intermediate ‘loading’ scene - it doesn’t necessarily need to display anything, just acts as a bridge.

In my ‘game’ scene I would do this:

[lua]

_G.nextScene = “game”

 storyboard.gotoScene(“loading”, “fromTop”, 100)

[/lua]

And this is my loading.lua file:

[lua]

module(…, package.seeall)

local storyboard = require(“storyboard”)

local scene = storyboard.newScene()


– STORYBOARD FUNCTIONS


– Called when the scene’s view does not exist:

function scene:createScene(event)

    local localGroup = self.view

    local r = display.newRect(0, 0, 700, 564)

    localGroup:insert®

    r:setFillColor(0,0,0)

end

– Called BEFORE scene has moved onscreen:

function scene:willEnterScene(event)

    local group = self.view

end

– Called immediately after scene has moved onscreen:

function scene:enterScene(event)

    local group = self.view

    local previous = storyboard.getPrevious()

    if previous ~= “main” and previous then

        storyboard.removeScene(previous)

    end

    local ready = function()

        storyboard.gotoScene(_G.nextScene, “fromBottom”, 400); end

    end

    timer.performWithDelay(50, ready)

end

– Called when scene is about to move offscreen:

function scene:exitScene(event)

    local group = self.view

end

– Called AFTER scene has finished moving offscreen:

function scene:didExitScene(event)

    local group = self.view

end

– Called prior to the removal of scene’s “view” (display group)

function scene:destroyScene(event)

    local group = self.view

end

– Called if/when overlay scene is displayed via storyboard.showOverlay()

function scene:overlayBegan(event)

    local group = self.view

    local overlay_scene = event.sceneName – overlay scene name

end

– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()

function scene:overlayEnded(event)

    local group = self.view

    local overlay_scene = event.sceneName – overlay scene name

end


– END OF YOUR IMPLEMENTATION


scene:addEventListener(“createScene”, scene)

scene:addEventListener(“willEnterScene”, scene)

scene:addEventListener(“enterScene”, scene)

scene:addEventListener(“exitScene”, scene)

scene:addEventListener(“didExitScene”, scene)

scene:addEventListener(“destroyScene”, scene)

scene:addEventListener(“overlayBegan”, scene)

scene:addEventListener(“overlayEnded”, scene)


return scene

[/lua]

I’ve always used an intermediate level progression type screen too.  But pretty much all my apps pre-date the reloadScene() call anyway that I would do that in.  But still, reloadScene should work.  You’re not getting any errors?

Sorry for the late reply. No I don’t get any errors. Like I said, the print(“abc”) is working so I know the listener is firing. But the reloadScene is not. I even put a print(“def”) AFTER the reloadScene line and it prints that. It just ignores the reload scene.

Any thoughts anyone??

local function onbtnPlayAgainEvent( event ) local phase = event.phase if "ended" == phase then print( "abc" ) storyboard.reloadScene() print( "def" ) end end

Well, I just got a reply back from support. I am doing things completely wrong it turns out. I somehow deleted all of the scene functions including createscene and enterscene. I just added the entire code for the game in the lua file and did not use any of the functions. I’m sorry but I’m confused on where everything goes for these functions. Where do I put all of the game coding? Which scene function? Does some go in the createscene and some in the enterscene? and where do i call purge scene based on the reply below?

Thanks for your help!!

Reply from support:  (which was appreciated!)

I’m sorry but we cannot debug your project code. I did a quick look and
see that your sceneGame.lua file does not follow the requirements for
creating a proper storyboard scene. Your code is not setting the
scene.view and there is no createScene or enterScene functions.

I would suggest creating a new “scene” project and that will generate
the template you need for creating a valid storyboard project.

I also looked at your original test code in your other bug report and
the reason you are not seeing the reload is because the createScene
function is only called when the scene is initially created. Since you
didn’t call purgeScene, createScene is not called upon a reload. If you
add a reload listener, you will see that it’s working correctly.

You can read more here (see the Gotchas section).

http://docs.coronalabs.com/daily/api/library/storyboard/reloadScene.html

Warren, see below a blank storyboard template that I have set up. Your game code (touch listeners, enterFrame listeners, game logic, functions etc) goes above all the storyboard functions. Within createScene you load all your display objects, and add any touch listeners you need (the code for which will be higher up in the lua file). Within enterScene you can start timers or add runtime listeners, and remember to cancel/remove these in willExitScene.

[lua]

module(…, package.seeall)


–LIBRARIES


local storyboard = require(“storyboard”)

local scene = storyboard.newScene()


–DISPLAY GROUPS


local localGroup = display.newGroup()


–LOCAL VARIABLES


local scene = storyboard.newScene()


–OBJECT DECLARATIONS



–GAME FUNCTIONS


local gameLoop = function ()

end


– STORYBOARD FUNCTIONS


– Called when the scene’s view does not exist:

function scene:createScene(event)

    localGroup = self.view

end

– Called BEFORE scene has moved onscreen:

function scene:willEnterScene(event)

    local group = self.view

end

– Called immediately after scene has moved onscreen:

function scene:enterScene(event)

    local group = self.view

    local ready = function()

       local previous = storyboard.getPrevious()

       if previous ~= “main” and previous then

            storyboard.removeScene(previous)

        end

    end

    timer.performWithDelay(1000, ready)

    Runtime:addEventListener(“enterFrame”,gameLoop)

end

– Called when scene is about to move offscreen:

function scene:exitScene(event)

    local group = self.view

end

– Called AFTER scene has finished moving offscreen:

function scene:didExitScene(event)

    local group = self.view

    Runtime:removeEventListener(“enterFrame”,gameLoop)

end

– Called prior to the removal of scene’s “view” (display group)

function scene:destroyScene(event)

    local group = self.view

end

– Called if/when overlay scene is displayed via storyboard.showOverlay()

function scene:overlayBegan(event)

    local group = self.view

    local overlay_scene = event.sceneName – overlay scene name

end

– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()

function scene:overlayEnded(event)

    local group = self.view

    local overlay_scene = event.sceneName – overlay scene name

end


– END OF YOUR IMPLEMENTATION


scene:addEventListener(“createScene”, scene)

scene:addEventListener(“willEnterScene”, scene)

scene:addEventListener(“enterScene”, scene)

scene:addEventListener(“exitScene”, scene)

scene:addEventListener(“didExitScene”, scene)

scene:addEventListener(“destroyScene”, scene)

scene:addEventListener(“overlayBegan”, scene)

scene:addEventListener(“overlayEnded”, scene)


return scene

[/lua]

Hi again,

Do you use the windows or mac version of corona? I am posting the windows version of my game. I still don’t have it right though. And I know some of the coding is bad but it kind of works! :slight_smile: I’m still learning…

You pull back on the horse shoe the cowboy is holding and it is thrown to knock over the beer cans. Still need to adjust the power and all more.

Warren

http://www.watchyourbabysitter.com/throw7.zip

Is that the latest version of the code? When I run it the game is in portrait (as set in build.settings), the background scrolls away, the cans fall everywhere and when the cowboy comes on screen you can’t grab the horseshoe.

I’m running on mac but that shouldn’t make any difference to how the code runs.

That is the latest. The app is in portrait mode because the other screens that this goes to must be portrait. So I had to set the gravity to another direction and rotate the images by 90 degrees. It works for me in the simulator and device this way.

Here are a couple of things.

1.  You cannot purge the scene you are currently in.

2.  Are you saying storyboard.gotoScene(“somescene”) doesn’t work?

I tried different variations of the code and also without the purge in there. What I am trying to do is reload the scene to play again. I tried the reload and also the gotoScene. The print action worked but nothing with the scene. So I know the listener is working. I have this written for an android although it mat work for apple but have not yet tried it. I can let you try and see youself with the game but I don’t want to post a link public for everyone to see. I have been trying to a couple weeks to get this to work. It is a basic throwing game like angry birds but another theme with one try. It’s a mini game to go with an app. After all of the objects stop moving based on their velocity I show a button to play again. I also need to add a button to return to the app so I need to use the gotoScene method also.

Thanks for your help!

Warren

Does anyone know why the storyboard.reloadScene() not work here? I also added a new scene and tried the storyboard.loadScene(“scenehere”) and the gotoScene and neither worked. Why would it be disabled or not working in the button listener? I am really stuck here…

Thanks,

Warren

My solution is usually to have an intermediate ‘loading’ scene - it doesn’t necessarily need to display anything, just acts as a bridge.

In my ‘game’ scene I would do this:

[lua]

_G.nextScene = “game”

 storyboard.gotoScene(“loading”, “fromTop”, 100)

[/lua]

And this is my loading.lua file:

[lua]

module(…, package.seeall)

local storyboard = require(“storyboard”)

local scene = storyboard.newScene()


– STORYBOARD FUNCTIONS


– Called when the scene’s view does not exist:

function scene:createScene(event)

    local localGroup = self.view

    local r = display.newRect(0, 0, 700, 564)

    localGroup:insert®

    r:setFillColor(0,0,0)

end

– Called BEFORE scene has moved onscreen:

function scene:willEnterScene(event)

    local group = self.view

end

– Called immediately after scene has moved onscreen:

function scene:enterScene(event)

    local group = self.view

    local previous = storyboard.getPrevious()

    if previous ~= “main” and previous then

        storyboard.removeScene(previous)

    end

    local ready = function()

        storyboard.gotoScene(_G.nextScene, “fromBottom”, 400); end

    end

    timer.performWithDelay(50, ready)

end

– Called when scene is about to move offscreen:

function scene:exitScene(event)

    local group = self.view

end

– Called AFTER scene has finished moving offscreen:

function scene:didExitScene(event)

    local group = self.view

end

– Called prior to the removal of scene’s “view” (display group)

function scene:destroyScene(event)

    local group = self.view

end

– Called if/when overlay scene is displayed via storyboard.showOverlay()

function scene:overlayBegan(event)

    local group = self.view

    local overlay_scene = event.sceneName – overlay scene name

end

– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()

function scene:overlayEnded(event)

    local group = self.view

    local overlay_scene = event.sceneName – overlay scene name

end


– END OF YOUR IMPLEMENTATION


scene:addEventListener(“createScene”, scene)

scene:addEventListener(“willEnterScene”, scene)

scene:addEventListener(“enterScene”, scene)

scene:addEventListener(“exitScene”, scene)

scene:addEventListener(“didExitScene”, scene)

scene:addEventListener(“destroyScene”, scene)

scene:addEventListener(“overlayBegan”, scene)

scene:addEventListener(“overlayEnded”, scene)


return scene

[/lua]

I’ve always used an intermediate level progression type screen too.  But pretty much all my apps pre-date the reloadScene() call anyway that I would do that in.  But still, reloadScene should work.  You’re not getting any errors?

Does anyone know what function is called in a storyboard for reloadScene? I’m still trying to get the code in the right places so this method works.

Thanks

Sorry for the late reply. No I don’t get any errors. Like I said, the print(“abc”) is working so I know the listener is firing. But the reloadScene is not. I even put a print(“def”) AFTER the reloadScene line and it prints that. It just ignores the reload scene.

Any thoughts anyone??

local function onbtnPlayAgainEvent( event ) local phase = event.phase if "ended" == phase then print( "abc" ) storyboard.reloadScene() print( "def" ) end end

Well, I just got a reply back from support. I am doing things completely wrong it turns out. I somehow deleted all of the scene functions including createscene and enterscene. I just added the entire code for the game in the lua file and did not use any of the functions. I’m sorry but I’m confused on where everything goes for these functions. Where do I put all of the game coding? Which scene function? Does some go in the createscene and some in the enterscene? and where do i call purge scene based on the reply below?

Thanks for your help!!

Reply from support:  (which was appreciated!)

I’m sorry but we cannot debug your project code. I did a quick look and
see that your sceneGame.lua file does not follow the requirements for
creating a proper storyboard scene. Your code is not setting the
scene.view and there is no createScene or enterScene functions.

I would suggest creating a new “scene” project and that will generate
the template you need for creating a valid storyboard project.

I also looked at your original test code in your other bug report and
the reason you are not seeing the reload is because the createScene
function is only called when the scene is initially created. Since you
didn’t call purgeScene, createScene is not called upon a reload. If you
add a reload listener, you will see that it’s working correctly.

You can read more here (see the Gotchas section).

http://docs.coronalabs.com/daily/api/library/storyboard/reloadScene.html