Hi!
I’m beginner in lua programming and I have problem with removing objects after changing storyboards. I would like to remove objects: zly, dobry and cewka , when new storyboard (restart.lua) will start. Could you help me, I don’t know how to do this. Thanks in advance
local storyboard = require ("storyboard") local scene = storyboard.newScene() local physics = require "physics" physics.start() physics.setGravity(0, 3) local ego = require "ego" local saveFile = ego.saveFile local loadFile = ego.loadFile motionx = 0; speed = 7; local ego = require "ego" local saveFile = ego.saveFile local loadFile = ego.loadFile local newestHighScore = loadFile( "filename.txt", highScore ) local newestHighScoreValue = tonumber(newestHighScore) local playerScore = 0 local highScore = newestHighScore local zycie = 3 function scene:createScene(event) local screenGroup = self.view tlo = display.newImage("t3.png") tlo.x = 250 tlo.y =150 screenGroup:insert(tlo) gui\_PlayerScore = display.newText("Wynik: ",-10,21,native.SystemFontBold,18) gui\_PlayerScore:setFillColor(255,255,255,10) screenGroup:insert(gui\_PlayerScore) gui\_Score = display.newText(""..playerScore,gui\_PlayerScore.x +45,21,native.SystemFontBold,18) gui\_Score:setFillColor(255,255,255,10) screenGroup:insert(gui\_Score) zycia = display.newText("Życia: ",460,21,native.SystemFontBold,18) zycia:setFillColor(255,255,255,10) screenGroup:insert(zycia) zyc = display.newText(""..zycie,zycia.x +45,21,native.SystemFontBold,18) zycia:setFillColor(255,255,255,10) screenGroup:insert(zyc) gui\_Best = display.newText("Najlepszy:",2, 40,native.SystemFont,18) gui\_Best:setFillColor(255,255,255,10) screenGroup:insert(gui\_Best) gui\_highScore = display.newText(""..highScore,gui\_Best.x + 65,40,native.SystemFont,18) gui\_highScore:setFillColor(255,255,255,10) screenGroup:insert(gui\_highScore) left = display.newImage ("left.png") left.x = 0; left.y = 280; screenGroup:insert(left) right = display.newImage ("right.png") right.x = 480; right.y = 282; screenGroup:insert(right) tesla = display.newImage("teslamaly.png") tesla.x = 250 tesla.y = 300 physics.addBody( tesla, "static", { filter = {maskBits = 2, categoryBits = 2} } ) screenGroup:insert(tesla) end local function networkListener( event ) if ( event.isError ) then print( "Network error!") else print ( "RESPONSE: " .. event.response ) end end function reset(event) if event.phase == "began" then storyboard.gotoScene("start", "fade", 500) end end function lefttouch() motionx = -speed; end function righttouch() motionx = speed; end local function movetesla (event) tesla.x = tesla.x + motionx; if tesla.x \>440 then motionx =0; end if tesla.x \< 25 then motionx = 0; end end local function stop (event) if event.phase =="ended" then motionx = 0; end end spawnTrue = true; zly = {} i = 1 local function getCoins(event) if event.phase == "ended" then backgroundMusic = audio.loadStream("zly.wav") backgroundMusicChannel = audio.play( backgroundMusic, { channel=2, loops=0} ) coin = event.target coin:removeSelf() coin = nil playerScore = playerScore + 1 gui\_Score.text = "" .. playerScore if playerScore \> newestHighScoreValue then highScore = playerScore gui\_highScore.text = "" .. highScore print ("saving highScore") saveFile( "filename.txt", highScore) end end end local function spawnStuff( event ) if spawnTrue == true then zly[i] = display.newImage("sad.png") zly[i].x = math.random( 0, 320 ) zly[i].y = -100 zly[i]:addEventListener( "collision", getCoins ) physics.addBody(zly[i], {bounce = .2, density = 1, filter = {maskBits = 2, 4, categoryBits = 2, 4}}) i = i + 1 return zly[i] end end timer.performWithDelay( 1900, spawnStuff, 10000 ) --------------------------------------------------------------------------------------------------- dobry = {} i = 1 function getDobry(event) if event.phase == "ended" then ass = audio.loadStream("dobry.wav") ass = audio.play( ass, { channel=3, loops=0} ) test = event.target test:removeSelf() test = nil playerScore = playerScore + 5 gui\_Score.text = "" .. playerScore end end spawnDwbry = true; function spawnDobry( event ) if spawnDwbry == true then dobry[i] = display.newImage("happy.png") dobry[i].x = math.random( 0, 320 ) physics.addBody(dobry[i], "dynamic") dobry[i].y = -100 dobry[i]:addEventListener( "touch", getDobry ) i = i + 1 end end timer.performWithDelay( 15000, spawnDobry, 10000 ) ------------------------------------------------------------------------------------ cewka = {} i = 1 function getCewka(event) if event.phase == "ended" then cewka1 = audio.loadStream("cewka.wav") cewka1 = audio.play( cewka1, { channel=4, loops=0} ) test = event.target test:removeSelf() test = nil zycie = zycie + 1 zyc.text ="" .. zycie end end dawid = true; function spawnCewka( event ) if dawid == true then cewka[i] = display.newImage("cewka.png") cewka[i].x = math.random( 0, 320 ) physics.addBody(cewka[i], "dynamic") cewka[i].y = -100 cewka[i]:addEventListener( "touch", getCewka ) i = i + 1 end end timer.performWithDelay( 50000, spawnCewka, 10000 ) ----------------------------------------------------------------------------------------------- liniaspawn = true; linia = {} i = 1 function getLinia(event) if event.phase == "ended" then obj = event.target obj:removeSelf() obj = nil zycie = zycie - 1 zyc.text ="" .. zycie end end function spawnLinie( event ) if liniaspawn == true then linia[i] = display.newImage("linia.png") linia[i].x = 50 linia[i].y = 400 linia[i]:addEventListener( "collision", getLinia ) physics.addBody( linia[i], "static", { filter = {maskBits = 2, categoryBits = 2} } ) i = i + 1 end end timer.performWithDelay( 1700, spawnLinie, 10000 ) function scene:enterScene(event) left:addEventListener("touch",lefttouch) right:addEventListener("touch",righttouch) Runtime:addEventListener("enterFrame", movetesla) Runtime:addEventListener("touch", stop ) end function scene:exitScene(event) left:removeEventListener("touch", lefttouch) right:removeEventListener("touch",righttouch) Runtime:removeEventListener("enterFrame", movetesla) Runtime:removeEventListener("touch", stop ) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene