Error when re-playing the game

Hey guys, when i play the game for the first time, it is all good, but when i choose the ooption “replay” which means to play the game again, but without exiting the game. When i choose replay this is the error i get:

Runtime error :  
  
maingame.lua:289: attempt to call method 'insert' a stack traceback  
  
in function 'insert' :  
maingame.lua:289: in function '\_listener'  
  
maingame.lua:309: attempt to call method 'insert' a stack traceback  
  
in function 'insert' :  
maingame.lua:309: in function '\_listener'  

this is the code in line 289:

gameGroup:insert( egg )

this is the code in line 309:

gameGroup:insert( enemyObject )

How do i solve this?
[import]uid: 193035 topic_id: 32894 reply_id: 332894[/import]

do you use any scene manager (director, storyboard…) to reload your game? [import]uid: 70635 topic_id: 32894 reply_id: 130712[/import]

Hi, your “egg” object is nil value that means that you probably create it on “scene create” metod or somthing and destroy it on exit. When you replay you are not calling “scene create” metod just other functions that use it

I hope you get the idea :slight_smile: [import]uid: 131764 topic_id: 32894 reply_id: 130713[/import]

Yes i use storyboard api. How do i solve that error? (Im beginner) Here is the code:

[code]
function scene:createScene( event )
local gameGroup = self.view
storyboard.removeScene( “loadgame” )
end

function scene:enterScene( event )
local gameGroup = self.view
– here comes all the game code and including 289 and 309 line
end

function scene:exitScene( event )
end

function scene:destroyScene( event )
end

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene[/code]

[import]uid: 193035 topic_id: 32894 reply_id: 130714[/import]

do you use any scene manager (director, storyboard…) to reload your game? [import]uid: 70635 topic_id: 32894 reply_id: 130712[/import]

@CrazyImp is right, it sounds like you may not be removing the scene when you exit. Can you show us the loading scene and the scene you go in between the game and the loading screen (if there is one)?

You should be calling (outside of the scene your removing) either

storyboard:removeAll()  
-- or  
storyboard:removeScene( sceneName )  

Also, are you properly removing items in destroyScene? Might help to see that if the above doesn’t fix the problem.

Finally, I did have issues reloading scenes on the public build (894), I think there’s a problem with storyboard in that build, my code stills freezes when reloading scenes if I revert Corona back to that. Unfortunately the only way to test that is to buy Corona and get a newer build, or have someone else run the code and see if the problem is still there. [import]uid: 181948 topic_id: 32894 reply_id: 130727[/import]

Hi, your “egg” object is nil value that means that you probably create it on “scene create” metod or somthing and destroy it on exit. When you replay you are not calling “scene create” metod just other functions that use it

I hope you get the idea :slight_smile: [import]uid: 131764 topic_id: 32894 reply_id: 130713[/import]

Yes i use storyboard api. How do i solve that error? (Im beginner) Here is the code:

[code]
function scene:createScene( event )
local gameGroup = self.view
storyboard.removeScene( “loadgame” )
end

function scene:enterScene( event )
local gameGroup = self.view
– here comes all the game code and including 289 and 309 line
end

function scene:exitScene( event )
end

function scene:destroyScene( event )
end

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene[/code]

[import]uid: 193035 topic_id: 32894 reply_id: 130714[/import]

@CrazyImp is right, it sounds like you may not be removing the scene when you exit. Can you show us the loading scene and the scene you go in between the game and the loading screen (if there is one)?

You should be calling (outside of the scene your removing) either

storyboard:removeAll()  
-- or  
storyboard:removeScene( sceneName )  

Also, are you properly removing items in destroyScene? Might help to see that if the above doesn’t fix the problem.

Finally, I did have issues reloading scenes on the public build (894), I think there’s a problem with storyboard in that build, my code stills freezes when reloading scenes if I revert Corona back to that. Unfortunately the only way to test that is to buy Corona and get a newer build, or have someone else run the code and see if the problem is still there. [import]uid: 181948 topic_id: 32894 reply_id: 130727[/import]

main.lua

display.setStatusBar( display.HiddenStatusBar )  
  
local storyboard = require ( "storyboard" )  
  
storyboard.gotoScene( "loadmainmenu" )  

mainmenu.lua

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local ui = require("ui")  
  
local btnAnim  
  
local btnSound = audio.loadSound( "btnSound.wav" )  
  
function scene:createScene( event )  
 local screenGroup = self.view  
  
 storyboard.removeScene( "maingame" )  
 storyboard.removeScene( "options" )  
end  
  
function scene:enterScene( event )  
 local screenGroup = self.view  
  
 local backgroundImage = display.newImage( "mainMenuBG.png")  
 backgroundImage.x = display.contentWidth/2; backgroundImage.y = display.contentHeight/2  
 screenGroup:insert( backgroundImage )  
  
 local playBtn  
  
 local onPlayTouch = function( event )  
 if event.phase == "release" then  
  
 audio.play( btnSound )  
 storyboard.gotoScene( "loadgame", "fade", 0 )  
  
 end  
 end  
  
 playBtn = ui.newButton{  
 defaultSrc = "playbtn.png",  
 defaultX = 180,  
 defaultY = 130,  
 overSrc = "playbtn-over.png",  
 overX = 180,  
 overY = 130,  
 onEvent = onPlayTouch,  
 id = "PlayButton",  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 size = 16,  
 emboss = false  
 }  
  
 playBtn.x = display.contentWidth/2; playBtn.y = -100  
 screenGroup:insert( playBtn )  
  
 btnAnim = transition.to( playBtn, { time=1000, y=display.contentHeight/2, transition=easing.inOutExpo } )  
  
 local optBtn  
  
 local onOptionsTouch = function( event )  
 if event.phase == "release" then  
  
 audio.play( btnSound )  
 storyboard.gotoScene( "options", "crossFade", 0 )  
  
 end  
 end  
  
 optBtn = ui.newButton{  
 defaultSrc = "optbtn.png",  
 defaultX = 180,  
 defaultY = 130,  
 overSrc = "optbtn-over.png",  
 overX = 180,  
 overY = 130,  
 onEvent = onOptionsTouch,  
 id = "OptionsButton",  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 size = 16,  
 emboss = false  
 }  
  
 optBtn.x = display.contentWidth/2; optBtn.y = -100  
 screenGroup:insert( optBtn )  
  
 btnAnim = transition.to( optBtn, { time=2000, y=display.contentHeight/2 + playBtn.contentHeight + 20, transition=easing.inOutExpo } )  
  
end  
  
function scene:exitScene()  
 if btnAnim then transition.cancel( btnAnim ); end  
end  
  
function scene:destroyScene( event )  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
return scene  

loadgame.lua

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local myTimer  
local loadingImage  
  
function scene:createScene( event )  
 local screenGroup = self.view  
  
 storyboard.removeScene( "mainmenu" )  
end  
  
function scene:enterScene( event )  
 local screenGroup = self.view  
  
 loadingImage = display.newImage( "loading2.png")  
 loadingImage.x = display.contentWidth/2; loadingImage.y = display.contentHeight/2  
 screenGroup:insert( loadingImage )  
  
 local changeScene = function()  
 storyboard.gotoScene( "maingame", "flipFadeOutIn", 0 )  
 end  
 myTimer = timer.performWithDelay( 1000, changeScene, 1 )  
  
end  
  
function scene:exitScene()  
 if myTimer then timer.cancel( myTimer ); end  
end  
  
function scene:destroyScene( event )  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
return scene  

loadmainmenu.lua

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local myTimer  
local loadingImage  
local vipSound = audio.loadSound( "vipSound.mp3" )  
  
function scene:createScene( event )  
 local screenGroup = self.view  
end  
  
function scene:enterScene( event )  
 local screenGroup = self.view  
  
 audio.play(vipSound)  
  
 loadingImage = display.newImage( "loading.png")  
 loadingImage.x = display.contentWidth/2; loadingImage.y = display.contentHeight/2  
 screenGroup:insert( loadingImage )  
  
 local goToMenu = function()  
 storyboard.gotoScene( "mainmenu", "fade", 0 )  
 end  
 myTimer = timer.performWithDelay( 2000, goToMenu, 1 )  
  
end  
  
function scene:exitScene()  
 if myTimer then timer.cancel( myTimer ); end  
end  
  
function scene:destroyScene( event )  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
return scene  

[import]uid: 193035 topic_id: 32894 reply_id: 130759[/import]

Yeah you aren’t removing the objects. Remove Scene will not remove all of the references to the objects because nothing is in your scene:destroyScene() events.

These should help:

http://developer.coronalabs.com/content/application-programming-guide-graphics-and-drawing#Removing_Objects_Properly

http://developer.coronalabs.com/reference/index/objectremoveself

Also, if it still doesn’t work after you remove the objects, post the line where the error is thrown (maingame.lua line 289). That should tell us specifically what isn’t being removed (or at least the first attempt to access the item that hasn’t been removed). [import]uid: 181948 topic_id: 32894 reply_id: 130771[/import]

main.lua

display.setStatusBar( display.HiddenStatusBar )  
  
local storyboard = require ( "storyboard" )  
  
storyboard.gotoScene( "loadmainmenu" )  

mainmenu.lua

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local ui = require("ui")  
  
local btnAnim  
  
local btnSound = audio.loadSound( "btnSound.wav" )  
  
function scene:createScene( event )  
 local screenGroup = self.view  
  
 storyboard.removeScene( "maingame" )  
 storyboard.removeScene( "options" )  
end  
  
function scene:enterScene( event )  
 local screenGroup = self.view  
  
 local backgroundImage = display.newImage( "mainMenuBG.png")  
 backgroundImage.x = display.contentWidth/2; backgroundImage.y = display.contentHeight/2  
 screenGroup:insert( backgroundImage )  
  
 local playBtn  
  
 local onPlayTouch = function( event )  
 if event.phase == "release" then  
  
 audio.play( btnSound )  
 storyboard.gotoScene( "loadgame", "fade", 0 )  
  
 end  
 end  
  
 playBtn = ui.newButton{  
 defaultSrc = "playbtn.png",  
 defaultX = 180,  
 defaultY = 130,  
 overSrc = "playbtn-over.png",  
 overX = 180,  
 overY = 130,  
 onEvent = onPlayTouch,  
 id = "PlayButton",  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 size = 16,  
 emboss = false  
 }  
  
 playBtn.x = display.contentWidth/2; playBtn.y = -100  
 screenGroup:insert( playBtn )  
  
 btnAnim = transition.to( playBtn, { time=1000, y=display.contentHeight/2, transition=easing.inOutExpo } )  
  
 local optBtn  
  
 local onOptionsTouch = function( event )  
 if event.phase == "release" then  
  
 audio.play( btnSound )  
 storyboard.gotoScene( "options", "crossFade", 0 )  
  
 end  
 end  
  
 optBtn = ui.newButton{  
 defaultSrc = "optbtn.png",  
 defaultX = 180,  
 defaultY = 130,  
 overSrc = "optbtn-over.png",  
 overX = 180,  
 overY = 130,  
 onEvent = onOptionsTouch,  
 id = "OptionsButton",  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 size = 16,  
 emboss = false  
 }  
  
 optBtn.x = display.contentWidth/2; optBtn.y = -100  
 screenGroup:insert( optBtn )  
  
 btnAnim = transition.to( optBtn, { time=2000, y=display.contentHeight/2 + playBtn.contentHeight + 20, transition=easing.inOutExpo } )  
  
end  
  
function scene:exitScene()  
 if btnAnim then transition.cancel( btnAnim ); end  
end  
  
function scene:destroyScene( event )  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
return scene  

loadgame.lua

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local myTimer  
local loadingImage  
  
function scene:createScene( event )  
 local screenGroup = self.view  
  
 storyboard.removeScene( "mainmenu" )  
end  
  
function scene:enterScene( event )  
 local screenGroup = self.view  
  
 loadingImage = display.newImage( "loading2.png")  
 loadingImage.x = display.contentWidth/2; loadingImage.y = display.contentHeight/2  
 screenGroup:insert( loadingImage )  
  
 local changeScene = function()  
 storyboard.gotoScene( "maingame", "flipFadeOutIn", 0 )  
 end  
 myTimer = timer.performWithDelay( 1000, changeScene, 1 )  
  
end  
  
function scene:exitScene()  
 if myTimer then timer.cancel( myTimer ); end  
end  
  
function scene:destroyScene( event )  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
return scene  

loadmainmenu.lua

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local myTimer  
local loadingImage  
local vipSound = audio.loadSound( "vipSound.mp3" )  
  
function scene:createScene( event )  
 local screenGroup = self.view  
end  
  
function scene:enterScene( event )  
 local screenGroup = self.view  
  
 audio.play(vipSound)  
  
 loadingImage = display.newImage( "loading.png")  
 loadingImage.x = display.contentWidth/2; loadingImage.y = display.contentHeight/2  
 screenGroup:insert( loadingImage )  
  
 local goToMenu = function()  
 storyboard.gotoScene( "mainmenu", "fade", 0 )  
 end  
 myTimer = timer.performWithDelay( 2000, goToMenu, 1 )  
  
end  
  
function scene:exitScene()  
 if myTimer then timer.cancel( myTimer ); end  
end  
  
function scene:destroyScene( event )  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
return scene  

[import]uid: 193035 topic_id: 32894 reply_id: 130759[/import]

Yeah you aren’t removing the objects. Remove Scene will not remove all of the references to the objects because nothing is in your scene:destroyScene() events.

These should help:

http://developer.coronalabs.com/content/application-programming-guide-graphics-and-drawing#Removing_Objects_Properly

http://developer.coronalabs.com/reference/index/objectremoveself

Also, if it still doesn’t work after you remove the objects, post the line where the error is thrown (maingame.lua line 289). That should tell us specifically what isn’t being removed (or at least the first attempt to access the item that hasn’t been removed). [import]uid: 181948 topic_id: 32894 reply_id: 130771[/import]

So in the destroy scene function i should put something like this ?? Because even with this, it gives me error :frowning:

function scene:destroyScene( event ) gameGroup:removeSelf() end

[import]uid: 193035 topic_id: 32894 reply_id: 130834[/import]

So in the destroy scene function i should put something like this ?? Because even with this, it gives me error :frowning:

function scene:destroyScene( event ) gameGroup:removeSelf() end

[import]uid: 193035 topic_id: 32894 reply_id: 130834[/import]