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