Unable to reload

Current status:

I have two files menu.lua and level1.lua . Menu file contains a button which go to scene level1.

The counter is created and once it completes the count an alert is visible. onclick of Play more buttons it goes back to the menu file.

On clicking again on this button, I want the scene to be reloaded. while it’s showing the last state.

I am confused after reading all reload and scene threads.

Thanks in Advance

Below is  code:


– level1.lua


local storyboard = require( “storyboard” )

local scene = storyboard.newScene()

– include Corona’s “physics” library

local physics = require “physics”

physics.start(); physics.pause()


– forward declarations and other locals

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

function levelfinished()

               local loose=“you win”

               local buttonLabels= {“Ok”,“Play more”}           

  local function onFinish( event )

    if “clicked” == event.action then

        local i = event.index

        if 1 == i then

                – Do nothing; dialog will simply dismiss

        elseif 2 == i then

            scene.view:removeSelf()

            --storyboard.reloadScene(“level1”,“fade”,1500)

            print(“play more”)

                                                          

                    --timer.cancel(event.source)

                    --scene.view:removeSelf()

                    --storyboard.removeAll()

                   – storyboard.purgeScene(“level1”)                      

               storyboard.gotoScene( “menu”, “fade”, 500 )

               

        end

    end

end 

                 native.showAlert(loose, “Game finish”, buttonLabels, onFinish) 

end

 --countdown timer

counter= 0

 – display text for timer

        local gametimer =display.newText(counter,display.contentWidth-60,30)

        gametimer.size = 16

local function countdown (event)

      local timertext= display.newText(“Timer”,display.contentWidth-60,10)

      local i = event.count

      print (“i=”… i)

      – print(“cratecount=”… fallingcrate.event.count)

      gametimer.text= i

      if i>= 6 then

         local timestop = timer.cancel(event.source)

         levelfinished()

      end     

           

       

end

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

function scene:createScene( event )

    local group = self.view

    – create a grey rectangle as the backdrop

    local background = display.newRect( 0, 0, screenW, screenH )

    background:setFillColor( 128 )

    

    – make a crate (off-screen), position it, and rotate slightly

    local crate = display.newImageRect( “crate.png”, 90, 90 )

    crate.x, crate.y = 160, -100

    --crate.rotation = 15

    

    – add physics to the crate

    physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )

    

    – create a grass object and add physics (with custom shape)

    local grass = display.newImageRect( “grass.png”, screenW, 82 )

    grass:setReferencePoint( display.BottomLeftReferencePoint )

    grass.x, grass.y = 0, display.contentHeight

    

    – define a shape that’s slightly shorter than image bounds (set draw mode to “hybrid” or “debug” to see)

    local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }

    physics.addBody( grass, “static”, { friction=0.3, shape=grassShape } )

            

    – all display objects must be inserted into group

    group:insert( background )

    group:insert( grass)

    group:insert( crate )

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

    local group = self.view

    

    physics.start()

    

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

    local group = self.view       

    print(“exit”)        

    physics.stop()

    

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:

function scene:destroyScene( event )

    local group = self.view    

    package.loaded[physics] = nil

    physics = nil

end

function scene:willEnterScene(evnt)

    print(“rload”)

 end

local counttimer=timer.performWithDelay(1000, countdown,60)


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

scene:addEventListener(“willEnterScene”, scene)


return scene

Hopefully someone else who knows will reply because I am having the same issues. I was told from my code sample that I did not have the code in the right functions of the storyboard page. I was also told I did not have my objects added to the storyboard’s .view which I am not familiar with anyways.

If anyone else knows can you reply and also tell how to add the items to the .view of a storyboard?

I see you have code in the createScene. I don’t know if the code in this function is called again on the reload either. It may be only once when you start the scene. This is all confusing to me also.

Warren

First of all, it really helps when you post code to include it in the proper tags:  [lua] and [/lua] (take out the spaces inside the brackets).  This is preserve your indents and makes it easier to read.

From what I can tell, there is code floating in the main chunk that will not get re-executed when the scene reloads.  These lines in particular:

[lua]

physics.start(); physics.pause()


– forward declarations and other locals

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

– and

–countdown timer

counter= 0

 – display text for timer

        local gametimer =display.newText(counter,display.contentWidth-60,30)

        gametimer.size = 16

– and

local counttimer=timer.performWithDelay(1000, countdown,60)

[/lua]

Storyboard scenes are just regular old .lua files that get “required” in.  When a file is required the first time, code in it’s main chunk gets run once.  If that code gets required again, the module’s chunk is not re-executed.  So that timer won’t fire the 2nd time around. 

Any thing that needs reset or re-run on reload needs to happen in the enterScene() function.  It can trigger your functions in the main chunk, but it’s the point of execution on reload.  The createScene() function will only fire if the scene has been purged, so on a reload, you can’t really expect it to run again.  So put anything necessary to reset the scene in the enterScene() function

Hopefully someone else who knows will reply because I am having the same issues. I was told from my code sample that I did not have the code in the right functions of the storyboard page. I was also told I did not have my objects added to the storyboard’s .view which I am not familiar with anyways.

If anyone else knows can you reply and also tell how to add the items to the .view of a storyboard?

I see you have code in the createScene. I don’t know if the code in this function is called again on the reload either. It may be only once when you start the scene. This is all confusing to me also.

Warren

First of all, it really helps when you post code to include it in the proper tags:  [lua] and [/lua] (take out the spaces inside the brackets).  This is preserve your indents and makes it easier to read.

From what I can tell, there is code floating in the main chunk that will not get re-executed when the scene reloads.  These lines in particular:

[lua]

physics.start(); physics.pause()


– forward declarations and other locals

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

– and

–countdown timer

counter= 0

 – display text for timer

        local gametimer =display.newText(counter,display.contentWidth-60,30)

        gametimer.size = 16

– and

local counttimer=timer.performWithDelay(1000, countdown,60)

[/lua]

Storyboard scenes are just regular old .lua files that get “required” in.  When a file is required the first time, code in it’s main chunk gets run once.  If that code gets required again, the module’s chunk is not re-executed.  So that timer won’t fire the 2nd time around. 

Any thing that needs reset or re-run on reload needs to happen in the enterScene() function.  It can trigger your functions in the main chunk, but it’s the point of execution on reload.  The createScene() function will only fire if the scene has been purged, so on a reload, you can’t really expect it to run again.  So put anything necessary to reset the scene in the enterScene() function