Reseting Score

My problem is that when i exit scene, (loase) and get back to it (new game). i see my old score + objects start fall like they werent ever stopped.

so i create score and lives:

 

score = 0 live = 3 local Wynik = display.newText("Score: ",halfW,10 ) Wynik.x = 30 Wynik.y = 5 local scoreText = display.newText(score,0,10 ) scoreText:setFillColor( 20, 10, 0 ) scoreText.x = 60 scoreText.y = 5 local liveText = display.newText(live,halfW,10 ) liveText:setFillColor( 20, 10, 0 ) liveText.x = 280 liveText.y = 5 local zycia = display.newText("Lives:",halfW,10 ) zycia.x = 255

now add them too group

 

sceneGroup:insert( bkg ) sceneGroup:insert( Wynik ) scoreGroup:insert( scoreText ) scoreGroup:insert( liveText ) sceneGroup:insert( zycia )

then i have function too reset them

 

local function resetAll() score = 0 live = 3 end

problem is, that when i start new game even when i use resetAll

local function gameOver() resetAll() composer.gotoScene("menu","fade", 500) end

my score/live is set like old one, and when i score, or loase live then it update

like i have score 20 i hit object and i get 1, i never see 0. Same with live.

  • my objects dont generate from start. They should fall from top off screen at start, but they are like in middle etc.

You can use composer.removeHidden() or composer.removeScene() to remove the previous scene from memory which will run the code from start the next time you call that scene.

https://docs.coronalabs.com/api/library/composer/index.html

now i have nil problem. “table index is nil.”
Game restart from scratch, but after few sec, this happens.

were should i put composer.removeScene()? 

I generally put composer.removeScene() just before I call composer.gotoScene():

composer.removeScene(“game”) – assuming I’m going to game.lua

composer.gotoScene(“game”, { effect=“crossFade”, time=500 } )

Rob

ehhh…
 

now i have: attempt to call method ïnsert “nil” value. 
 

i think its about groupScene:insert() stuff…

You need to provide more information.  A copy/paste of the entire error from your console.log would be great.  That should mention the module and line number where the problem happened. Copy and pasting that code around the error message would also be quite helpful.

Rob

The Error

18:00:18.870  ERROR: Runtime error

18:00:18.870 level1.lua:192: attempt to call method ‘insert’ (a nil value)

18:00:18.870  stack traceback:

18:00:18.870   level1.lua:192: in function ‘_listener’

18:00:18.870   ?: in function <?:167>

18:00:18.870   ?: in function <?:169>

lines in error are thoes that create staff (new object)

Ok so i got menu.lua i which i got removeScene()

local function onPlayBtnRelease() -- go to level1.lua scene composer.removeScene("level1") composer.gotoScene( "level1", "fade", 500 ) return true -- indicates successful touch end

then i got level1.lua. My main game

1st i create stuff and add too scene group.

 

function scene:create( event ) local sceneGroup = self.view -- Start the physics engine local physics = require( "physics" ) physics.start() -- Calculate half the screen width and height halfW = display.contentWidth\*0.5 halfH = display.contentHeight\*0.5 -- Set the background local bkg = display.newImage(sceneGroup,"night\_sky.png", halfW, halfH ) -- Score score = 0 live = 3 local Wynik = display.newText(sceneGroup,"Score: ",halfW,10 ) Wynik.x = 30 Wynik.y = 5 local scoreText = display.newText(sceneGroup,score,0,10 ) scoreText:setFillColor( 20, 10, 0 ) scoreText.x = 60 scoreText.y = 5 local liveText = display.newText(sceneGroup,live,halfW,10 ) liveText:setFillColor( 20, 10, 0 ) liveText.x = 280 liveText.y = 5 local zycia = display.newText(sceneGroup,"Lives:",halfW,10 ) zycia.x = 255 zycia.y = 5

got my gameover (too nexr scene)

 

local function gameOver() composer.gotoScene("menu","fade",500) end

Object creation stuff:

 

local function addNewObject() local startX = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) local a = math.random(1,12) if(a\<=5)then local rock = display.newImage("rock.png", startX, -300) physics.addBody( rock ) rock.enterFrame = offscreen Runtime:addEventListener( "enterFrame", rock ) rock:addEventListener( "touch", rockTouched ) sceneGroup:insert(rock) elseif(a==12)then local b=math.random(1,5) if (b==1) then g="bad-blue.png" elseif(b==2)then g="bad-green.png" elseif(b==3)then g="bad-purple.png" elseif(b==4)then g="bad-red.png" else g="bad-yellow.png" end local badjelly = display.newImage(g, startX, -300) physics.addBody( badjelly ) badjelly.enterFrame = offscreen Runtime:addEventListener( "enterFrame", badjelly ) badjelly:addEventListener( "touch", badJellyTouched ) sceneGroup:insert(badjelly) elseif(a==10)then local jar = display.newImage("jar.png", startX, -300) physics.addBody( jar ) jar.enterFrame = offscreen Runtime:addEventListener( "enterFrame", jar ) jar:addEventListener( "touch", jarTouched ) sceneGroup:insert(jar) elseif(a==11)then local powerJelly = display.newImage("Power Jelly.png", startX, -300) physics.addBody( powerJelly ) powerJelly.enterFrame = offscreen Runtime:addEventListener( "enterFrame", powerJelly ) powerJelly:addEventListener( "touch", superJellyTouched ) sceneGroup:insert(powerJelly) else local b=math.random(1,5) if (b==1) then g="jelly-blue.png" elseif(b==2)then g="jelly-green.png" elseif(b==3)then g="jelly-purple.png" elseif(b==4)then g="jelly-red.png" else g="jelly-yellow.png" end local jelly = display.newImage(g, startX, -300) physics.addBody( jelly ) jelly.enterFrame = jellyoffscreen Runtime:addEventListener( "enterFrame", jelly ) jelly:addEventListener( "touch", jellyTouched ) sceneGroup:insert(jelly) end end -- Keep adding a new balloon or bomb every 0.5 seconds local f = timer.performWithDelay( 500, addNewObject, 0 )

and some touch events

 

local function jellyTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = score + 1 scoreText.text = score end end local function rockTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() if(live==0)then gameOver() else live = live-1 liveText.text = live end end end local function badJellyTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = 0 scoreText.text = score if(live==0)then gameOver() else live = live-1 liveText.text = live end end end local function superJellyTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = score\*2 scoreText.text = score end end local function jarTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() if(live\<3)then live = live+1 liveText.text = live end end end -- Delete objects which has fallen off the bottom of the screen local function offscreen(self, event) if(self.y == nil) then return end if(self.y \> display.contentHeight + 20) then Runtime:removeEventListener( "enterFrame", self ) self:removeSelf() end end local function jellyoffscreen(self, event) if(self.y == nil) then return end if(self.y \> display.contentHeight + 20) then if(live==0)then gameOver() else live = live-1 liveText.text = live end Runtime:removeEventListener( "enterFrame", self ) self:removeSelf() end end

What is line 192 of level1.lua?

190 Runtime:addEventListener( "enterFrame", jelly ) 191 jelly:addEventListener( "touch", jellyTouched ) 192 sceneGroup:insert(jelly)

Is the function: addNewObject() inside the scene:create() function or outside of it? If it’s outside (and it should be), sceneGroup doesn’t exist. You have to create it at the top of the function:

local function&nbsp;addNewObject() &nbsp; &nbsp; &nbsp;local sceneGroup = scene.view ... end

i have it, in scene:create(). 

Can you post your complete scene?

sure

 

local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view -- Start the physics engine local physics = require( "physics" ) physics.start() -- Calculate half the screen width and height halfW = display.contentWidth\*0.5 halfH = display.contentHeight\*0.5 -- Set the background local bkg = display.newImage(sceneGroup,"night\_sky.png", halfW, halfH ) -- Score score = 0 live = 3 local Wynik = display.newText(sceneGroup,"Score: ",halfW,10 ) Wynik.x = 30 Wynik.y = 5 local scoreText = display.newText(sceneGroup,score,0,10 ) scoreText:setFillColor( 20, 10, 0 ) scoreText.x = 60 scoreText.y = 5 local liveText = display.newText(sceneGroup,live,halfW,10 ) liveText:setFillColor( 20, 10, 0 ) liveText.x = 280 liveText.y = 5 local zycia = display.newText(sceneGroup,"Lives:",halfW,10 ) zycia.x = 255 zycia.y = 5 local function jellyTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = score + 1 scoreText.text = score end end local function gameOver() composer.gotoScene("menu","fade",500) end local function rockTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() if(live==0)then gameOver() else live = live-1 liveText.text = live end end end local function badJellyTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = 0 scoreText.text = score if(live==0)then gameOver() else live = live-1 liveText.text = live end end end local function superJellyTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = score\*2 scoreText.text = score end end local function jarTouched(event) if ( event.phase == "began" ) then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() if(live\<3)then live = live+1 liveText.text = live end end end -- Delete objects which has fallen off the bottom of the screen local function offscreen(self, event) if(self.y == nil) then return end if(self.y \> display.contentHeight + 20) then Runtime:removeEventListener( "enterFrame", self ) self:removeSelf() end end local function jellyoffscreen(self, event) if(self.y == nil) then return end if(self.y \> display.contentHeight + 20) then if(live==0)then gameOver() else live = live-1 liveText.text = live end Runtime:removeEventListener( "enterFrame", self ) self:removeSelf() end end local function addNewObject() local startX = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) local a = math.random(1,12) if(a\<=5)then local rock = display.newImage("rock.png", startX, -300) physics.addBody( rock ) rock.enterFrame = offscreen Runtime:addEventListener( "enterFrame", rock ) rock:addEventListener( "touch", rockTouched ) sceneGroup:insert(rock) elseif(a==12)then local b=math.random(1,5) if (b==1) then g="bad-blue.png" elseif(b==2)then g="bad-green.png" elseif(b==3)then g="bad-purple.png" elseif(b==4)then g="bad-red.png" else g="bad-yellow.png" end local badjelly = display.newImage(g, startX, -300) physics.addBody( badjelly ) badjelly.enterFrame = offscreen Runtime:addEventListener( "enterFrame", badjelly ) badjelly:addEventListener( "touch", badJellyTouched ) sceneGroup:insert(badjelly) elseif(a==10)then local jar = display.newImage("jar.png", startX, -300) physics.addBody( jar ) jar.enterFrame = offscreen Runtime:addEventListener( "enterFrame", jar ) jar:addEventListener( "touch", jarTouched ) sceneGroup:insert(jar) elseif(a==11)then local powerJelly = display.newImage("Power Jelly.png", startX, -300) physics.addBody( powerJelly ) powerJelly.enterFrame = offscreen Runtime:addEventListener( "enterFrame", powerJelly ) powerJelly:addEventListener( "touch", superJellyTouched ) sceneGroup:insert(powerJelly) else local b=math.random(1,5) if (b==1) then g="jelly-blue.png" elseif(b==2)then g="jelly-green.png" elseif(b==3)then g="jelly-purple.png" elseif(b==4)then g="jelly-red.png" else g="jelly-yellow.png" end local jelly = display.newImage(g, startX, -300) physics.addBody( jelly ) jelly.enterFrame = jellyoffscreen Runtime:addEventListener( "enterFrame", jelly ) jelly:addEventListener( "touch", jellyTouched ) sceneGroup:insert(jelly) end end -- Keep adding a new balloon or bomb every 0.5 seconds local f = timer.performWithDelay( 500, addNewObject, 0 ) end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

I don’t see anything obvious that would cause this. You’re missing  your scene:show() function, but that shouldn’t cause this error. The only thing is if there is if something is trying to remove this scene while it’s in the process of being created, but would think you would have more errors.

Maybe zip up the whole project and lets look at the whole thing.

Rob

I can’t get the file to download. Can you use Google Drive or Dropbox?

The code now works like once.

  1. i play

  2. loase - > menu

  3. Start Game

  4. Loase -> error

i fixed the problem
it was all about this in scene:destroy()

 

local sceneGroup = self.view package.loaded[physics] = nil physics = nil

now when i get rid off it game works fine

You can use composer.removeHidden() or composer.removeScene() to remove the previous scene from memory which will run the code from start the next time you call that scene.

https://docs.coronalabs.com/api/library/composer/index.html

now i have nil problem. “table index is nil.”
Game restart from scratch, but after few sec, this happens.

were should i put composer.removeScene()? 

I generally put composer.removeScene() just before I call composer.gotoScene():

composer.removeScene(“game”) – assuming I’m going to game.lua

composer.gotoScene(“game”, { effect=“crossFade”, time=500 } )

Rob