scene transitions/reloading break variables, don't reload correctly? code included.

i have a game with a main menu, a “level”, and an ad page.
 

i have it set up so they click a play button on the main menu.  It takes them to the game just fine, and the game functions just fine too, but the pause menu with it doesn’t.  I have a pause button, which is a widget, that triggers a function that displays a rectangle and 3 more widgets (restart, menu, and resume).

here is the code for the functions the pause menu buttons have:

[lua]

local displayedColor = display.newText(“0”, 300, 20, native.systemFont, 40)

displayedColor:setTextColor(0, 0, 0)

local displayedFinger = display.newText(“0”, 350, 20, native.systemFont, 40)

displayedFinger:setTextColor(0, 0, 0)

local colorshown = display.newRoundedRect(150, 25, 80, 80, 12)

colorshown.strokeWidth = 3

colorshown:setFillColor(255, 255, 255)

colorshown:setStrokeColor(0, 0, 0)

local function onmenuBtnRelease()

    

    pauseGroup:removeSelf()

    pauseGroup = nil

    

    colorshown:removeSelf()  --THIS IS LINE 117

    colorshown = nil

    fingerShown:removeSelf()

    fingerShown = nil

    

    storyboard.gotoScene( “main_menu” )

    

    return true    – indicates successful touch

end

local function onrestartBtnRelease()

    

    pauseGroup:removeSelf()

    pauseGroup = nil

    

    colorshown:removeSelf()

    colorshown = nil

    fingerShown:removeSelf()

    fingerShown = nil

    

    – go to ad scene

    storyboard.gotoScene( “reload_ad” )

    

    return true    – indicates successful touch

end

local function onresumeBtnRelease()

    

    pauseGroup:removeSelf()

    pauseGroup = nil

    

    return true    – indicates successful touch

end

– ‘onRelease’ event listener for sPlyrBtn

local function onpauseBtnPress()

    

    local myRoundedRect = display.newRoundedRect(134, 201, 500, 350, 12)

    myRoundedRect.strokeWidth = 8

    myRoundedRect:setFillColor(214, 214, 214)

    myRoundedRect:setStrokeColor(111, 111, 111)

    local pauseLabel = display.newText(“Paused”, 0, 225, native.systemFont, 90)

    pauseLabel:setTextColor(50, 50, 50)

    pauseLabel:setReferencePoint( CenterReferencePoint )

    pauseLabel.x = halfW

    pauseLabel.y = 250

    

    – create a widget button

    restartBtn = widget.newButton{

        label="",

        labelColor = { default={255}, over={128} },

        fontSize = 0,

        defaultFile=“restart_btn.png”,

        overFile=“restart_btn-over.png”,

        width=100, height=100,

        onRelease = onrestartBtnRelease

    }

    restartBtn:setReferencePoint( display.CenterReferencePoint )

    restartBtn.x = 215

    restartBtn.y = 475

    

    – create a widget button

    resumeBtn = widget.newButton{

        label="",

        labelColor = { default={255}, over={128} },

        fontSize = 0,

        defaultFile=“play_btn.png”,

        overFile=“play_btn-over.png”,

        width=100, height=100,

        onRelease = onresumeBtnRelease

    }

    resumeBtn:setReferencePoint( display.CenterReferencePoint )

    resumeBtn.x = 550

    resumeBtn.y = 475

    

    – create a widget button

    menuBtn = widget.newButton{

        label=“Menu”,

        labelColor = { default={255}, over={128} },

        fontSize = 55,

        defaultFile=“menubtn.png”,

        overFile=“menubtn-over.png”,

        width=200, height=100,

        onRelease = onmenuBtnRelease

    }

    menuBtn:setReferencePoint( display.CenterReferencePoint )

    menuBtn.x = halfW

    menuBtn.y = 475

    

    pauseGroup = display.newGroup()

    

    pauseGroup:insert( myRoundedRect )

    pauseGroup:insert( pauseLabel )

    pauseGroup:insert( restartBtn )

    pauseGroup:insert( resumeBtn )

    pauseGroup:insert( menuBtn )

    

    return true    – indicates successful touch

end

[/lua]

the reload_ad scene just has code to go back to the game scene as i haven’t implemented ads yet.

when i use the restart button, it does what it is suppose to do and goes quickly to the ad page and back to the game.  The problem is, if i try to use the menu/restart buttons again, i get an error saying:

1_player.lua:117: attempt to index upvalue ‘colorshown’ (a nil value)

I was under the impression if you exited a scene, it got completly deleted and when you re enter it, it would behave exactly the same as the first time you used it.

Anyone have any thoughts?

There is another button in the actual game that modifies the color of colorshown and if i press it, the game will crash

it is sort of like this problem i think: http://forums.coronalabs.com/topic/36473-best-way-to-use-local-variables-in-the-main-chunk-area-of-a-storyboard-module/

but i tried both 

  1. function scene:enterScene( event )
  2.     – == If there was a previous screen like the help - totally remove it from memory
  3.     if storyboard.getPrevious() ~= nil then
  4.         storyboard.purgeScene(storyboard.getPrevious())
  5.         storyboard.removeScene(storyboard.getPrevious())
  6.     end
  7. end.

–and this

 

 

  1. local storyboard = require( “storyboard” )
  2. storyboard.purgeOnSceneChange = true 

–but it still didn’t work

What part of storyboard is your above code within?  createScene?   If so, createScene isn’t always called every time the scene is placed on screen.  

You are correct that you should use storyboard.purgeOnSceneChange = true, however, where are you placing this line?  Also, what does your main.lua look like?   

it was above the main create scene function.  I reshuffled it after your comment about only running once and original problem is solved

main.lua is stock from game template

the purge line is under my local storyboard = require( “storyboard” ) line

reshuffling surfaced 2 new problems:

some of my updating text keeps priting over itself…anyway to fix this???

and some of my displayed objects don’t disappear with the scene.

triple post???

why isn’t my post at the bottom???

sry for all those extra posts…The first one loaded above the topic post so i tried again…

This is how I usually make my main.lua when using storyboard:

display.setStatusBar( display.HiddenStatusBar ) local storyboard = require "storyboard" storyboard.purgeOnSceneChange = true storyboard.gotoScene( "scene-MainMenu" )

EDIT:  Yeah, the forum is acting a little weird.  Replies are showing right away, have to refresh manually.

ok, thanks.  I think I got it fixed now, but im still going to make my main like yours to be safe.

one last problem though: one of my rectangles that is made under the create scene function shows up before the first “transition into the scene” has finished, like it isn’t in the group.  Any good reason for this?

Are you sure you added it to the group?  Is it the same rect in your original post (colorshown)?  Because in your code above I don’t see that you’ve added it to the scene’s group.

yes, it is the same one in the above code, but i moved it into the main onCreate function to fix the crash issue.  When i did this, I also added it to the group, along with a few other objects i moved into the main code.  The others work, but this one doesn’t.

Is there a limit on the number of things in a group?  The group it goes in has 32 items in it.

would it be possible to put the code into the enterScene function?  That may work if it is called after the transition is finished.

it is sort of like this problem i think: http://forums.coronalabs.com/topic/36473-best-way-to-use-local-variables-in-the-main-chunk-area-of-a-storyboard-module/

but i tried both 

  1. function scene:enterScene( event )
  2.     – == If there was a previous screen like the help - totally remove it from memory
  3.     if storyboard.getPrevious() ~= nil then
  4.         storyboard.purgeScene(storyboard.getPrevious())
  5.         storyboard.removeScene(storyboard.getPrevious())
  6.     end
  7. end.

–and this

 

 

  1. local storyboard = require( “storyboard” )
  2. storyboard.purgeOnSceneChange = true 

–but it still didn’t work

What part of storyboard is your above code within?  createScene?   If so, createScene isn’t always called every time the scene is placed on screen.  

You are correct that you should use storyboard.purgeOnSceneChange = true, however, where are you placing this line?  Also, what does your main.lua look like?   

it was above the main create scene function.  I reshuffled it after your comment about only running once and original problem is solved

main.lua is stock from game template

the purge line is under my local storyboard = require( “storyboard” ) line

reshuffling surfaced 2 new problems:

some of my updating text keeps priting over itself…anyway to fix this???

and some of my displayed objects don’t disappear with the scene.

triple post???

why isn’t my post at the bottom???

sry for all those extra posts…The first one loaded above the topic post so i tried again…

This is how I usually make my main.lua when using storyboard:

display.setStatusBar( display.HiddenStatusBar ) local storyboard = require "storyboard" storyboard.purgeOnSceneChange = true storyboard.gotoScene( "scene-MainMenu" )

EDIT:  Yeah, the forum is acting a little weird.  Replies are showing right away, have to refresh manually.

ok, thanks.  I think I got it fixed now, but im still going to make my main like yours to be safe.

one last problem though: one of my rectangles that is made under the create scene function shows up before the first “transition into the scene” has finished, like it isn’t in the group.  Any good reason for this?

Are you sure you added it to the group?  Is it the same rect in your original post (colorshown)?  Because in your code above I don’t see that you’ve added it to the scene’s group.