Cleaning scene in storyboard problem

I have been using your Corona SDK for creating my game and it is close to being completed, but I have run into an annoying problem.

I am using storyboard to move from one scene to the next and it works just fine as long as I do not open a previous scene. For example, 

if I open scene01 and then restart the same scene, there is no error.

if I open scene01 and then open scene02, there is no problem

But if I open scene01, goto scene02 and then go back to scene01, I get this error.

 

o45j.jpg

"attempt to call method ‘setSequence’ (a nil value)"

If you see the video, I go from level01 to level02 and as soon as I reopen level01, this error pops up (where the video ends).

I have tried purging scenes, removing scenes and even purge all but I do not understand why the code for the previous scene still loads when in the new scene. Does anyone have any idea why this is happening? Any help will be greatly appreciated, because all the rest of my game is complete and this is stopping its launch.

Code for level01

 

-- requires local gameState = require( "gamestate" ) local storyboard = require "storyboard" local scene = storyboard.newScene() local physics = require "physics" physics.start() physics.setGravity( 0, 4 ) gameState.level = 1 gameState.nextlevel = gameState.level + 1 -- Globals local maxfuel = 1000 local fuel = maxfuel local maxhealth = 100 local health = maxhealth local damage = 0 local exhaustSound local explosionSound local beepSound local levelcomplete = false local explosionplayed = false; --Scenery variables local background local cloud00 local cloud01 local ground local horizon local padoptions local padData --Game objects local pad local padanimating local finishpad local finishpadanimating local options local sequenceData local rocketship local rocketshipAnimating --UI elements local healthicon local healthbar local barbody01 local barbody02 local fuelicon local fuelbar local success local countdown function scene:createScene(event) local screenGroup = self.view exhaustSound = audio.loadSound( "exhaust.wav" ) explosionSound = audio.loadSound( "explosion.wav" ) beepSound = audio.loadSound( "beep.wav" ) -- initializing background background = display.newImage("background00.png") background.x = display.contentWidth/2 background.y = display.contentHeight/2 screenGroup:insert(background) -- initialize clouds cloud00 = display.newImage("cloud00.png") cloud00.x = display.contentWidth/2 cloud00.y = 100 screenGroup:insert(cloud00) cloud01 = display.newImage("cloud01.png") cloud01.x = display.contentWidth/4 \* 3 cloud01.y = 200 screenGroup:insert(cloud01) -- initializing ground ground = display.newImage("ground00.png") ground.myName = "ground" ground.x = display.contentWidth/2 ground.y = display.contentHeight - ground.contentHeight/2 physics.addBody(ground, "static", {bounce=0.3, friction=0.5}) screenGroup:insert(ground) -- initializing left wall leftwall = display.newImage("boundary.png") leftwall.myName = "leftwall" leftwall.x = -50 leftwall.y = display.contentHeight - leftwall.contentHeight/2 physics.addBody(leftwall, "static", {bounce=0.3, friction=0.5}) screenGroup:insert(leftwall) -- initializing right wall rightwall = display.newImage("boundary.png") rightwall.myName = "rightwall" rightwall.x = display.contentWidth + 50 rightwall.y = display.contentHeight - rightwall.contentHeight/2 physics.addBody(rightwall, "static", {bounce=0.3, friction=0.5}) screenGroup:insert(rightwall) -- initializing horizon horizon = display.newImage("horizon00.png") horizon.x = display.contentWidth/2 horizon.y = display.contentHeight - ground.contentHeight - horizon.contentHeight/2 screenGroup:insert(horizon) padoptions = { width = 80, height = 30, numFrames = 2, sheetContentWidth = 160, --width of original 1x size of entire sheet sheetContentHeight = 30 --height of original 1x size of entire sheet } padData = { { name="blinking", start=1, count=2, time=2000 } } pad = graphics.newImageSheet("pad.png", padoptions) padanimating = display.newSprite( pad, padData ) padanimating.x = display.contentWidth/4 padanimating.y = display.contentHeight - ground.contentHeight - padanimating.contentHeight/2 padanimating:setSequence("blinking") padanimating:play() padanimating.myName = "pad" physics.addBody(padanimating, "static", {density=.3, bounce=0, friction=0.8}) screenGroup:insert(padanimating) finishpad = graphics.newImageSheet("pad.png", padoptions) finishpadanimating = display.newSprite( finishpad, padData ) finishpadanimating.x = display.contentWidth\* 3/4 finishpadanimating.y = display.contentHeight - ground.contentHeight - finishpadanimating.contentHeight/2 finishpadanimating:setSequence("blinking") finishpadanimating:play() finishpadanimating.myName = "finishpad" physics.addBody(finishpadanimating, "static", {density=.3, bounce=0, friction=0.8}) screenGroup:insert(finishpadanimating) options = { width = 60, height = 120, numFrames = 3, sheetContentWidth = 180, --width of original 1x size of entire sheet sheetContentHeight = 120 --height of original 1x size of entire sheet } sequenceData = { { name="flying", start=2, count=2, time=100 }, { name="notflying", start=1, count=1, time=0, loopCount=0 } } rocketship = graphics.newImageSheet("rocketship.png", options) rocketshipAnimating = display.newSprite( rocketship, sequenceData ) --rocketShape = { 0,-60, 19,0, 30,11, 27,60, -27, 60, -30,11, -19,0 } rocketshipAnimating.x = display.contentWidth/4 rocketshipAnimating.y = display.contentHeight - ground.contentHeight - padanimating.contentHeight - rocketshipAnimating.contentHeight/2 - 50 rocketshipAnimating:setSequence("flying") rocketshipAnimating:play() rocketshipAnimating.myName = "rocketship" rocketshipAnimating.isAlive = true --physics.addBody(rocketshipAnimating, "static", {density=.2, bounce=0.01, friction=0.2, shape=rocketShape }) physics.addBody(rocketshipAnimating, "static", {density=.2, bounce=0.01, friction=0.2 }) screenGroup:insert(rocketshipAnimating) rocketshipIntro = transition.to(rocketshipAnimating, {time = 4000, y = display.contentHeight - ground.contentHeight - padanimating.contentHeight - rocketshipAnimating.contentHeight/2, onComplete = RocketReady}) explosionoptions = { width = 64, height = 64, numFrames = 25, sheetContentWidth = 1600, --width of original 1x size of entire sheet sheetContentHeight = 64 --height of original 1x size of entire sheet } explosionData = { { name="exploding", start=1, count=25, time=1000, loopCount=1 } } explosion = graphics.newImageSheet("Explosion.png", explosionoptions) explosionAnimating = display.newSprite( explosion, explosionData ) explosionAnimating:setSequence("exploding") explosionAnimating:play() explosionAnimating.myName = "explosion" explosionAnimating.isVisible = false screenGroup:insert(explosionAnimating) -- initialise UI elements success = display.newImage("Success.png") success.x = display.contentWidth/2 success.y = display.contentHeight/2 success.isVisible = false screenGroup:insert(success) healthicon = display.newImage("healthicon.png") healthicon.x = 2\*healthicon.contentWidth/2 healthicon.y = display.contentHeight - 2 \* healthicon.contentHeight/2 screenGroup:insert(healthicon) healthbar = display.newImage("healthbar.png") healthbar.x = 2\*healthbar.contentWidth/2 healthbar.y = display.contentHeight - 2 \* healthicon.contentHeight healthbar.anchorY = healthicon.contentHeight screenGroup:insert(healthbar) barbody01 = display.newImage("barbody.png") barbody01.x = 2\*barbody01.contentWidth/2 barbody01.y = display.contentHeight - 7 \* healthicon.contentHeight screenGroup:insert(barbody01) fuelicon = display.newImage("fuelicon.png") fuelicon.x = display.contentWidth - 2\*fuelicon.contentWidth/2 fuelicon.y = display.contentHeight - 2 \* fuelicon.contentHeight/2 screenGroup:insert(fuelicon) fuelbar = display.newImage("fuelbar.png") fuelbar.x = display.contentWidth - 2\*fuelicon.contentWidth/2 fuelbar.y = display.contentHeight - 2 \* fuelicon.contentHeight fuelbar.anchorY = fuelicon.contentHeight screenGroup:insert(fuelbar) barbody02 = display.newImage("barbody.png") barbody02.x = display.contentWidth - 2\*fuelicon.contentWidth/2 barbody02.y = display.contentHeight - 7 \* fuelicon.contentHeight screenGroup:insert(barbody02) countdownoptions = { frames = { -- FRAME 1: { --all parameters below are required for each frame x = 0, y = 0, width = 120, height = 120 }, -- FRAME 2: { x = 120, y = 0, width = 120, height = 120 }, -- FRAME 3: { x = 240, y = 0, width = 120, height = 120 }, -- FRAME 4: { x = 360, y = 0, width = 360, height = 120 }, }, --optional parameters; used for dynamic resolution support sheetContentWidth = 720, sheetContentHeight = 120 } countdownData = { { name="countdown", start=1, count=4, time=4000, loopCount=1 } } countdown = graphics.newImageSheet("countdown.png", countdownoptions) countdownAnimating = display.newSprite( countdown, countdownData ) countdownAnimating.x = display.contentWidth/2 countdownAnimating.y = display.contentHeight/2 countdownAnimating:setSequence("countdown") countdownAnimating:play() countdownAnimating.myName = "countdown" countdownAnimating.isVisible = true screenGroup:insert(countdownAnimating) end ------------------ -- ON COLLISION -- ------------------ ----------------------------- -- ROTATION AND PUSH CONTROLS ----------------------------- ------------------- -- TOUCH SCREEN -- ------------------- function TouchScreen(event) if event.phase == "began" then if (event.x \< display.contentWidth/4) and (event.y \> display.contentHeight/2) then rocketshipAnimating.enterFrame = RotateLeft end if (event.x \> display.contentWidth\* 3/4) and (event.y \> display.contentHeight/2) then rocketshipAnimating.enterFrame = RotateRight end if (event.x \< display.contentWidth/4) and (event.y \<= display.contentHeight/2) then rocketshipAnimating.enterFrame = PushLeft end if (event.x \> display.contentWidth\* 3/4) and (event.y \<= display.contentHeight/2) then rocketshipAnimating.enterFrame = PushRight end if (event.x \>= display.contentWidth/4) and event.x \<= (display.contentWidth\* 3/4) then if fuel \> 0 then rocketshipAnimating.enterFrame = ActivateJets rocketshipAnimating:setSequence("flying") rocketshipAnimating:play() end end if(rocketshipAnimating.isAlive == true) then Runtime:addEventListener("enterFrame", rocketshipAnimating) end end if event.phase == "ended" then rocketshipAnimating:setSequence("notflying") rocketshipAnimating:play() Runtime:removeEventListener("enterFrame", rocketshipAnimating) end end ------------------- -- ENTER SCENE -- ------------------- function scene:enterScene(event) local prior\_scene = storyboard.getPrevious() storyboard.purgeScene( prior\_scene ) maxfuel = 1000 fuel = maxfuel maxhealth = 100 health = maxhealth damage = 0 levelcomplete = false explosionplayed = false; Runtime:addEventListener( "postCollision", onPostCollision ) cloud00.enterFrame = scrollCloud00 Runtime:addEventListener("enterFrame", cloud00) cloud01.enterFrame = scrollCloud01 Runtime:addEventListener("enterFrame", cloud01) Runtime:addEventListener("touch", TouchScreen) end function scene:exitScene(event) Runtime:removeEventListener("enterFrame", rocketshipAnimating) Runtime:removeEventListener( "postCollision", onPostCollision ) Runtime:removeEventListener("enterFrame", cloud00) Runtime:removeEventListener("enterFrame", cloud01) Runtime:removeEventListener("touch", TouchScreen) end function scene:destroyScene(event) local screenGroup = self.view end function scene:didExitScene( event ) storyboard.purgeScene( "level01" ) end scene:addEventListener( "didExitScene" ) scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene ----------------------- -- CODE FOR LEVEL 2-- -----------------------

– requires

local gameState = require( “gamestate” )

local storyboard = require “storyboard”

local scene = storyboard.newScene()

local physics = require “physics”

–physics.setDrawMode(“hybrid”)

physics.start()

physics.setGravity( 0, 4 )

gameState.level = 2

gameState.nextlevel = gameState.level + 1

– Globals

local maxfuel = 1000

local fuel = maxfuel

local maxhealth = 100

local health = maxhealth

local damage = 0

local exhaustSound

local explosionSound

local beepSound

local levelcomplete = false

local explosionplayed = false;

–Scenery variables

local background

local cloud00

local cloud01

local ground

local horizon

local padoptions

local padData

–Game objects

local pad

local padanimating

local finishpad

local finishpadanimating

local options

local sequenceData

local rocketship 

local rocketshipAnimating

–UI elements

local healthicon

local healthbar

local barbody01

local barbody02

local fuelicon

local fuelbar

local success

local countdown

function scene:createScene(event)

    local screenGroup = self.view

    

    exhaustSound = audio.loadSound( “exhaust.wav” )

    explosionSound = audio.loadSound( “explosion.wav” )

    beepSound = audio.loadSound( “beep.wav” )

    

    – initializing background

    background = display.newImage(“background00.png”)

    background.x = display.contentWidth/2

    background.y = display.contentHeight/2

    screenGroup:insert(background)

    – initialize clouds

    cloud00 = display.newImage(“cloud00.png”)

    cloud00.x = display.contentWidth/2

    cloud00.y = 100

    screenGroup:insert(cloud00)

    cloud01 = display.newImage(“cloud01.png”)

    cloud01.x = display.contentWidth/4 * 3

    cloud01.y = 200

    screenGroup:insert(cloud01)

    – initializing ground 

    ground = display.newImage(“ground00.png”)

    ground.myName = “ground”

    ground.x = display.contentWidth/2

    ground.y = display.contentHeight - ground.contentHeight/2

    physics.addBody(ground, “static”, {bounce=0.3, friction=0.5})

    screenGroup:insert(ground)

    

    – initializing left wall 

    leftwall = display.newImage(“boundary.png”)

    leftwall.myName = “leftwall”

    leftwall.x = -50

    leftwall.y = display.contentHeight - leftwall.contentHeight/2

    physics.addBody(leftwall, “static”, {bounce=0.3, friction=0.5})

    screenGroup:insert(leftwall)

    

    – initializing right wall 

    rightwall = display.newImage(“boundary.png”)

    rightwall.myName = “rightwall”

    rightwall.x = display.contentWidth + 50

    rightwall.y = display.contentHeight - rightwall.contentHeight/2

    physics.addBody(rightwall, “static”, {bounce=0.3, friction=0.5})

    screenGroup:insert(rightwall)

    – initializing horizon 

    horizon = display.newImage(“horizon00.png”)

    horizon.x = display.contentWidth/2

    horizon.y = display.contentHeight - ground.contentHeight - horizon.contentHeight/2

    screenGroup:insert(horizon)

    

    – initializing rock01 

    rock01 = display.newImage(“stone03.png”)

    rock01.myName = “rock01”

    rock01.x = rock01.contentWidth/2

    rock01.y = display.contentHeight - ground.contentHeight - rock01.contentHeight/2

    physics.addBody(rock01, “dynamic”, {density=1.0, bounce=0.01, friction=0.9})

    screenGroup:insert(rock01)

    

    rock02 = display.newImage(“stone03.png”)

    rock02.myName = “rock02”

    rock02.x = rock02.contentWidth/2

    rock02.y = display.contentHeight - ground.contentHeight - rock01.contentHeight - rock02.contentHeight/2

    physics.addBody(rock02, “dynamic”, {density=1.0, bounce=0.01, friction=0.9})

    screenGroup:insert(rock02)

    

    rock03 = display.newImage(“stone04.png”)

    rock03.myName = “rock03”

    rock03.x = rock03.contentWidth/2

    rock03.y = display.contentHeight - ground.contentHeight - rock01.contentHeight - rock02.contentHeight - rock03.contentHeight/2

    physics.addBody(rock03, “dynamic”, {density=1.0, bounce=0.01, friction=0.9})

    screenGroup:insert(rock03)

    

    rock04 = display.newImage(“stone02.png”)

    rock04.myName = “rock04”

    rock04.x = rock04.contentWidth/2

    rock04.y = display.contentHeight - ground.contentHeight - rock01.contentHeight - rock02.contentHeight - rock03.contentHeight - rock04.contentHeight/2

    physics.addBody(rock04, “dynamic”, {density=1.0, bounce=0.01, friction=0.9})

    screenGroup:insert(rock04)

    

    rock05 = display.newImage(“stone04.png”)

    rock05.myName = “rock05”

    rock05.x = rock05.contentWidth/2

    rock05.y = display.contentHeight - ground.contentHeight - rock01.contentHeight - rock02.contentHeight - rock03.contentHeight - rock04.contentHeight - rock05.contentHeight/2

    physics.addBody(rock05, “dynamic”, {density=2.0, bounce=0.01, friction=0.9})

    screenGroup:insert(rock05)

    padoptions =

    {

        width = 80,

        height = 30,

        numFrames = 2,

        sheetContentWidth = 160,  --width of original 1x size of entire sheet

        sheetContentHeight = 30  --height of original 1x size of entire sheet

    }

    padData = {

    

      { name=“blinking”, start=1, count=2, time=2000 }

    

    }

    finishpad = graphics.newImageSheet(“pad.png”, padoptions)

    finishpadanimating = display.newSprite( finishpad, padData )

    finishpadanimating.x = display.contentWidth* 1/4

    finishpadanimating.y = display.contentHeight - ground.contentHeight - finishpadanimating.contentHeight/2

    finishpadanimating:setSequence(“blinking”)

    finishpadanimating:play()

    finishpadanimating.myName = “finishpad”

    physics.addBody(finishpadanimating, “static”, {density=.3, bounce=0, friction=0.8})

    screenGroup:insert(finishpadanimating)

    

    options =

    {

        width = 60,

        height = 120,

        numFrames = 3,

        sheetContentWidth = 180,  --width of original 1x size of entire sheet

        sheetContentHeight = 120  --height of original 1x size of entire sheet

    }

    sequenceData = {

    

      { name=“flying”, start=2, count=2, time=100 },

      { name=“notflying”, start=1, count=1, time=0, loopCount=0 }

    

    }

    rocketship = graphics.newImageSheet(“rocketship.png”, options)

    rocketshipAnimating = display.newSprite( rocketship, sequenceData )

    --rocketShape = { 0,-60, 19,0, 30,11, 27,60, -27, 60, -30,11, -19,0 }

    rocketshipAnimating.x = display.contentWidth*1/4

    rocketshipAnimating.y = rocketshipAnimating.contentHeight/2 

    rocketshipAnimating:setSequence(“flying”)

    rocketshipAnimating:play()

    rocketshipAnimating.myName = “rocketship”

    rocketshipAnimating.isAlive = true

    --physics.addBody(rocketshipAnimating, “static”, {density=.2, bounce=0.01, friction=0.2, shape=rocketShape })

    physics.addBody(rocketshipAnimating, “static”, {density=.2, bounce=0.01, friction=0.2 })

    screenGroup:insert(rocketshipAnimating)

    

    rocketshipIntro = transition.to(rocketshipAnimating, {time = 4000, y = rocketshipAnimating.contentHeight/2 + 50, onComplete = RocketReady})

    

    explosionoptions =

    {

        width = 64,

        height = 64,

        numFrames = 25,

        sheetContentWidth = 1600,  --width of original 1x size of entire sheet

        sheetContentHeight = 64  --height of original 1x size of entire sheet

    }

    explosionData = { 

    

      { name=“exploding”, start=1, count=25, time=1000, loopCount=1 }

    

    }

    explosion = graphics.newImageSheet(“Explosion.png”, explosionoptions)

    explosionAnimating = display.newSprite( explosion, explosionData )

    explosionAnimating:setSequence(“exploding”)

    explosionAnimating:play()

    explosionAnimating.myName = “explosion”

    explosionAnimating.isVisible = false

    screenGroup:insert(explosionAnimating)

    – initialise UI elements

    

    success = display.newImage(“Success.png”)

    success.x = display.contentWidth/2

    success.y = display.contentHeight/2

    success.isVisible = false

    screenGroup:insert(success)

    

    healthicon = display.newImage(“healthicon.png”)

    healthicon.x = 2*healthicon.contentWidth/2

    healthicon.y = display.contentHeight - 2 * healthicon.contentHeight/2

    screenGroup:insert(healthicon)

    healthbar = display.newImage(“healthbar.png”)

    healthbar.x = 2*healthbar.contentWidth/2

    healthbar.y = display.contentHeight - 2 * healthicon.contentHeight

    healthbar.anchorY = healthicon.contentHeight

    screenGroup:insert(healthbar)

    barbody01 = display.newImage(“barbody.png”)

    barbody01.x = 2*barbody01.contentWidth/2

    barbody01.y = display.contentHeight - 7 * healthicon.contentHeight

    screenGroup:insert(barbody01)

    fuelicon = display.newImage(“fuelicon.png”)

    fuelicon.x = display.contentWidth - 2*fuelicon.contentWidth/2

    fuelicon.y = display.contentHeight - 2 * fuelicon.contentHeight/2

    screenGroup:insert(fuelicon)

    fuelbar = display.newImage(“fuelbar.png”)

    fuelbar.x = display.contentWidth - 2*fuelicon.contentWidth/2

    fuelbar.y = display.contentHeight - 2 * fuelicon.contentHeight

    fuelbar.anchorY = fuelicon.contentHeight

    screenGroup:insert(fuelbar)

    barbody02 = display.newImage(“barbody.png”)

    barbody02.x = display.contentWidth - 2*fuelicon.contentWidth/2

    barbody02.y = display.contentHeight - 7 * fuelicon.contentHeight

    screenGroup:insert(barbody02)

    

    countdownoptions =

    {

        frames =

    {

        – FRAME 1:

        {

            --all parameters below are required for each frame

            x = 0,

            y = 0,

            width = 120,

            height = 120

        },

        – FRAME 2:

        {

            x = 120,

            y = 0,

            width = 120,

            height = 120

        },

        – FRAME 3:

        {

            x = 240,

            y = 0,

            width = 120,

            height = 120

        },

        

        – FRAME 4:

        {

            x = 360,

            y = 0,

            width = 360,

            height = 120

        },

    },

    --optional parameters; used for dynamic resolution support

    sheetContentWidth = 720,

    sheetContentHeight = 120

    }

    countdownData = { 

    

      { name=“countdown”, start=1, count=4, time=4000, loopCount=1 }

    

    }

    countdown = graphics.newImageSheet(“countdown.png”, countdownoptions)

    countdownAnimating = display.newSprite( countdown, countdownData )

    countdownAnimating.x = display.contentWidth/2

    countdownAnimating.y = display.contentHeight/2 

    countdownAnimating:setSequence(“countdown”)

    countdownAnimating:play()

    countdownAnimating.myName = “countdown”

    countdownAnimating.isVisible = true

    screenGroup:insert(countdownAnimating)

end


– TOUCH SCREEN –


function TouchScreen(event)

    

    if event.phase == “began” then

        if (event.x < display.contentWidth/4) and (event.y > display.contentHeight/2) then

            rocketshipAnimating.enterFrame = RotateLeft

        end

        if (event.x > display.contentWidth* 3/4) and (event.y > display.contentHeight/2)   then

            rocketshipAnimating.enterFrame = RotateRight

        end

        if (event.x < display.contentWidth/4) and (event.y <= display.contentHeight/2) then

            rocketshipAnimating.enterFrame = PushLeft

        end

        if (event.x > display.contentWidth* 3/4) and (event.y <= display.contentHeight/2)   then

            rocketshipAnimating.enterFrame = PushRight

        end

        if (event.x >= display.contentWidth/4) and event.x  <= (display.contentWidth* 3/4)  then

        

            if fuel > 0 then

                

                rocketshipAnimating.enterFrame = ActivateJets

                rocketshipAnimating:setSequence(“flying”)

                rocketshipAnimating:play()

                

            end    

        end

        if(rocketshipAnimating.isAlive == true) then

            Runtime:addEventListener(“enterFrame”, rocketshipAnimating)

        end

    end

    

    if event.phase == “ended” then

        rocketshipAnimating:setSequence(“notflying”)

        rocketshipAnimating:play()

        Runtime:removeEventListener(“enterFrame”, rocketshipAnimating)

    end

end


– ROCKETSHIP DEAD –


function RocketshipDead()

    if explosionplayed == false then

        explosionplayed = true

        rocketshipAnimating.BodyType = “static”

        rocketshipAnimating.isVisible = false

        explosionAnimating.x = rocketshipAnimating.x

        explosionAnimating.y = rocketshipAnimating.y

        explosionAnimating.isVisible = true

        explosionAnimating:play()

        local explosionChannel = audio.play( explosionSound )

        timer.performWithDelay(3000, GameOver, 1)

    end

end

function GameOver()    

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

end


– ENTER SCENE –


function scene:enterScene(event)

    local prior_scene = storyboard.getPrevious()

    storyboard.purgeScene( prior_scene )

    

    maxfuel = 1000

    fuel = maxfuel

    maxhealth = 100

    health = maxhealth

    damage = 0

    levelcomplete = false

    explosionplayed = false;

    

    Runtime:addEventListener( “postCollision”, onPostCollision )

    

    cloud00.enterFrame = scrollCloud00

    Runtime:addEventListener(“enterFrame”, cloud00)

    

    cloud01.enterFrame = scrollCloud01

    Runtime:addEventListener(“enterFrame”, cloud01)

    

    Runtime:addEventListener(“touch”, TouchScreen)

end

    

function scene:exitScene(event)

    Runtime:removeEventListener(“enterFrame”, rocketshipAnimating)

    Runtime:removeEventListener( “postCollision”, onPostCollision )

    Runtime:removeEventListener(“enterFrame”, cloud00)

    Runtime:removeEventListener(“enterFrame”, cloud01)

    Runtime:removeEventListener(“touch”, TouchScreen)

end

function scene:destroyScene(event)

    local screenGroup = self.view

end

function scene:didExitScene( event )

    storyboard.purgeScene( “level02” )

end

scene:addEventListener( “didExitScene” )

scene:addEventListener(“createScene”, scene)

scene:addEventListener(“enterScene”, scene)

scene:addEventListener(“exitScene”, scene)

scene:addEventListener(“destroyScene”, scene)

return scene

Code for Level 2

 

The fact that this error is showing up means that for some reason the image/sprite that you are calling (the one for which you want to change sequence) does not exist anymore. So your question is: why is this image being removed, and why is it not being re-created when you reload your scene…

I looked quickly at the code, but since its quite lengthy it would take longer to be able to find anything. Suggestion: try simplifying it a bit, removing bit by bit until you can narrow it down.

Yes, the problem is with the sprite in Level02. Let me explain the events once again

I go to level01, then move on to level02 and then when I return to level01, I get this error being reported in level02.

Since I purge level02, this error shows up, because the sprite is no longer in the memory, but the code tries to play it and gets this error. I think I am purging unnecessarily in many places, so that might be the problem. I think you can ignore the entire code and just look for the problem in the enter and exit scene functions. I on the other hand remove the purges and retry

OK, I figured it out… You should not purge scenes when jumping between different levels. Only purge a previous scene if you want to restart it. This worked for me… cannot say if this is the right approach however… I hope someone explains properly.

OK, the culprit is the purge scene

If I purge scene and restart … all is good

If I purge scene and go to next scene, then all is good

If I purge scene, go to next scene and then come back to the first scene, I get this error

Any help?

Why are you purging scene 1 on didExitScene?  Why not just purge the previous scene in createScene like you do the first time?

Also, do you really have different functionality on every level that requires separate logic?    You may want to find a way to extract scene specific stuff into a table and read in a master PlayScene.    

I got kind of desperate trying to fix the bug, so I tried many things that I found online.

Your suggestion for master PlayScene is good. I will look into it… in fact now that I think about it, that will end up fixing my problem. Thank you so much!

The fact that this error is showing up means that for some reason the image/sprite that you are calling (the one for which you want to change sequence) does not exist anymore. So your question is: why is this image being removed, and why is it not being re-created when you reload your scene…

I looked quickly at the code, but since its quite lengthy it would take longer to be able to find anything. Suggestion: try simplifying it a bit, removing bit by bit until you can narrow it down.

Yes, the problem is with the sprite in Level02. Let me explain the events once again

I go to level01, then move on to level02 and then when I return to level01, I get this error being reported in level02.

Since I purge level02, this error shows up, because the sprite is no longer in the memory, but the code tries to play it and gets this error. I think I am purging unnecessarily in many places, so that might be the problem. I think you can ignore the entire code and just look for the problem in the enter and exit scene functions. I on the other hand remove the purges and retry

OK, I figured it out… You should not purge scenes when jumping between different levels. Only purge a previous scene if you want to restart it. This worked for me… cannot say if this is the right approach however… I hope someone explains properly.

OK, the culprit is the purge scene

If I purge scene and restart … all is good

If I purge scene and go to next scene, then all is good

If I purge scene, go to next scene and then come back to the first scene, I get this error

Any help?

Why are you purging scene 1 on didExitScene?  Why not just purge the previous scene in createScene like you do the first time?

Also, do you really have different functionality on every level that requires separate logic?    You may want to find a way to extract scene specific stuff into a table and read in a master PlayScene.    

I got kind of desperate trying to fix the bug, so I tried many things that I found online.

Your suggestion for master PlayScene is good. I will look into it… in fact now that I think about it, that will end up fixing my problem. Thank you so much!