Error on storyboard.gotoScene: Random Crashes or upvalue errors

My game randomly freezes when changing between levels.  I have four separate options:

1. Reload the level
2. Exit the Level
3. Change Items in the Level
4. Next Level

Each one runs a code as shown below (comments included – and there are a lot more lines than this – this is a sample).  Sometimes they work perfectly and other times it errors out by either freezing, or giving me an upvalue error related to a touch object that has a function associated with it. 

– Sample Code


– levela1.lua

local storyboard = require( “storyboard” )
local scene3 = storyboard.newScene()
storyboard.purgeOnSceneChange = true
storyboard.removeAll();

– include Corona’s “physics” library
local physics = require “physics”
physics.start();


– forward declarations and other locals

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


– BEGINNING OF YOUR IMPLEMENTATION

– NOTE: Code outside of listener functions (below) will only be executed once,
–   unless storyboard.removeScene() is called.


– physics.setDrawMode( “debug” )
– physics.setDrawMode( “hybrid” )

– Called when the scene’s view does not exist:
function scene3:createScene( event )
 local group = self.view

– Next is the declaration of variables, adding images and functions (touch, onCollision, etc.)


– This is the function that runs to clear objects and transition to the next scene - where it crashes

function exitlevel:touch ( event )

 if “began” == event.phase then
         exitlevel.isFocus = true
 
  print (“Checked exit level”);

– Move some objects off screen

  hero.x = 4000
  hero1.x = 4000

– Remove spritesheets

  spriteSheet1:dispose();
  spriteSheet1 = nil;

  – This will print “false”

  if (spriteSheet1) then
     print(“true”);
  else
     print(“false”);
  end;

  pwrgrn:removeSelf();
  pwrred:removeSelf();
  pwrgrn = nil;
  pwrred = nil;

– the removeself  and nil continues with about 45 more objects.  I’ve tried using the removal of a group and it caused the game to error out.  Not sure if this is related to some items in the group being removed earlier in the game as certain events execute.

  storyboard3.gotoScene( “menuwl”, “slideDown”, 3000 );

– This takes the player to a blank transition screen, which executes a remove all and remove scene (hopefully to clear the memory).  Then it either goes to another screen or relaunches this level (which seems to be the most stable function).

        elseif “ended” == phase or “cancelled” == phase then

            exitlevel.isFocus = false

        end

 end

 exitlevel:addEventListener(“touch”, exitlevel)


– End of the Implementation

end

– Called immediately after scene3 has moved onscreen:
function scene3:enterScene( event )
 local group = self.view
 
 physics.start()
 
end

– Called when scene3 is about to move offscreen:
function scene3:exitScene( event )
 local group = self.view
 
 physics.stop()
 
end

– If scene3’s view is removed, scene3:destroyScene() will be called just prior to:
function scene3:destroyScene( event )
 local group = self.view
 
 package.loaded[physics] = nil
 physics = nil
end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene3’s view does not exist
scene3:addEventListener( “createScene”, scene3 )

– “enterScene” event is dispatched whenever scene3 transition has finished
scene3:addEventListener( “enterScene”, scene3 )

– “exitScene” event is dispatched whenever before next scene3’s transition begins
scene3:addEventListener( “exitScene”, scene3 )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard3.purgeScene() or storyboard3.removeScene().

scene3:addEventListener( “destroyScene”, scene3 )


return scene3

What is storyboard3?

Why are you naming it scene3?  Are you trying to put them all in one file or are these separate files?

What is the specific upvalue error?  Is there more to the stack trace that goes with it?  

What is storyboard3?  I apologize the declarations for this are:

local storyboard3 = require( “storyboard” )
local scene3 = storyboard3.newScene()
storyboard3.purgeOnSceneChange = true
storyboard3.removeAll();

Why are you naming it scene3?  Are you trying to put them all in one file or are these separate files? 

I have three levels level1a.lua, level2a.lua, and level3a.lua.  I named the scenes scene1, scene2, and scene3.  Should I name them all scene?

What is the specific upvalue error?  Is there more to the stack trace that goes with it?  

It is random and sometimes it’s a physics upvalue error or an error related to one of my “touch” functions.  On one iteration it was always a physics error.  I used the object = nil; after removing the object, that seemed to fix it, but now I have the random upvalue error.  I can play the game, reload it, change scenes, and then randomly it stops with a blank screen, or throws an upvalue error.  75% of the time it freezes and goes to blank screen (some objects may or may not remain that should have cleared).  I have line prints throughout the code to show after lines have executed, and all the lines have executed.

Thanks for your help with this.

The scene object is unique per level as long as you’re putting a “local” in front of it so you would not need to put numbers on them.

Next this block of code:

storyboard.purgeOnSceneChange = true
storyboard.removeAll();

– include Corona’s “physics” library
local physics = require “physics”
physics.start();

will only execute once, the very first time the scene loads, though with that removeAll() there, if it’s in all scenes, then it might do what you expect but you would have to be consistent with it.  I find it better to call .removeScene() before I call gotoScene().

Without specific error’s its going to be hard to advise you on how to solve them.

I will try this and let you know how it goes.  Thanks.

What is storyboard3?

Why are you naming it scene3?  Are you trying to put them all in one file or are these separate files?

What is the specific upvalue error?  Is there more to the stack trace that goes with it?  

What is storyboard3?  I apologize the declarations for this are:

local storyboard3 = require( “storyboard” )
local scene3 = storyboard3.newScene()
storyboard3.purgeOnSceneChange = true
storyboard3.removeAll();

Why are you naming it scene3?  Are you trying to put them all in one file or are these separate files? 

I have three levels level1a.lua, level2a.lua, and level3a.lua.  I named the scenes scene1, scene2, and scene3.  Should I name them all scene?

What is the specific upvalue error?  Is there more to the stack trace that goes with it?  

It is random and sometimes it’s a physics upvalue error or an error related to one of my “touch” functions.  On one iteration it was always a physics error.  I used the object = nil; after removing the object, that seemed to fix it, but now I have the random upvalue error.  I can play the game, reload it, change scenes, and then randomly it stops with a blank screen, or throws an upvalue error.  75% of the time it freezes and goes to blank screen (some objects may or may not remain that should have cleared).  I have line prints throughout the code to show after lines have executed, and all the lines have executed.

Thanks for your help with this.

The scene object is unique per level as long as you’re putting a “local” in front of it so you would not need to put numbers on them.

Next this block of code:

storyboard.purgeOnSceneChange = true
storyboard.removeAll();

– include Corona’s “physics” library
local physics = require “physics”
physics.start();

will only execute once, the very first time the scene loads, though with that removeAll() there, if it’s in all scenes, then it might do what you expect but you would have to be consistent with it.  I find it better to call .removeScene() before I call gotoScene().

Without specific error’s its going to be hard to advise you on how to solve them.

I will try this and let you know how it goes.  Thanks.