Building a clean data form

How can I delete all objects from "game.lua" when I leave the game scene, all the data remains on the screen, how can I delete everything when I leave, and reset will return to its original position when I return to the game.

game.lua:

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() _W = display.contentWidth _H = display.contentHeight system.setIdleTimer(false); -- Prevent the app from becoming suspended local physics = require "physics" physics.start() clouts = true score = 0 speeda1 = 100 speedb1 = 150 function scene:createScene( event ) local group = self.view end function scene:enterScene( event ) local group = self.view --start drop zone if clouts then local badclout1 = {} local bad1Group = display.newGroup() local function spawnBC1() local badclouts1 = display.newImage("BCloud1.png") badclouts1.x = math.random(0, _W) physics.addBody( badclouts1, "dynamic", { density=.1, bounce=.1, friction=.2, radius=45 } ) badclouts1.name = "BCloud1" badclouts1.bodyType = "kinematic" badclouts1.isSensor = true badclouts1.y = math.random(-100, -50) badclouts1.index = #badclout1 + 1 bad1Group:insert(badclouts1) badclouts1.rotation = math.random(-10,10) -- Rotate the object badclouts1:setLinearVelocity(0, math.random(speeda1, speedb1)) -- Drop down badclout1[badclouts1.index] = badclouts1 tmrSpawn1 = timer.performWithDelay(math.random(spawna, spawnb), spawnBC1) return badclouts1 end tmrSpawn1 = timer.performWithDelay(math.random(1000, 10000), spawnBC1) local function removeBomb() for i, v in pairs(badclout1) do if badclout1[i].y >1000 then badclout1[i]:removeSelf() badclout1[i] = nil end end end Runtime:addEventListener("enterFrame", removeBomb) end -- end drop zone local function speatTimer() speeda1 = speeda1+1 speedb1 = speedb1+1 end local mainTimer = timer.performWithDelay( 550, speatTimer, 200 ) function gameOver() storyboard.gotoScene("restart", "fade", 400) end end function scene:exitScene( event ) local group = self.view Runtime:removeEventListener( "collision", onCollision ) Runtime:removeEventListener("accelerometer", onTilt) Runtime:removeEventListener("enterFrame", removeBomb) end function scene:destroyScene( event ) local group = self.view end function scene:overlayEnded( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) scene:addEventListener( "overlayEnded", scene ) return scene 

Regards, Kevin,

+4
source share
3 answers

if you use the storyboard and want to reset all the values ​​that you can delete the scene before moving to another scene, so that when you switch to the same scene again, everything will be created

 function scene:exitScene( event ) local group = self.view Runtime:removeEventListener( "collision", onCollision ) Runtime:removeEventListener("accelerometer", onTilt) Runtime:removeEventListener("enterFrame", removeBomb) storyboard.removeScene("SCENE TO REMOVE") end 

or do it after going to the stage

 function scene:enterScene( event ) storyboard.removeScene("SCENE TO REMOVE") end 

here is a link on how to implement it. http://www.coronalabs.com/blog/2012/07/31/storyboard-scene-purging-vs-removal/

+2
source

There are two ways to reset back to your object positions and variable data.

first - create a function like

 function resetGame() --your initial position and data values here end 

And name it whenever you need to reset your game, but you will manually specify reset values.

the second is the creation of a fictitious scene. a fictitious scene is a scene that redirects you back to the game scene, like this

Dummy

 function scene:createScene( event ) local group = self.view storyboard.gotoScene( "scenes.Game" ) --scene/Game.lua end 

to reset the values ​​of your objects and variables, but do not forget to delete all the listeners and put them in the exitScene() function when you go to the dummy scene .

+1
source

Since you are using a storyboard, why not just paste all of your displayed objects into a group:

  group:insert(someobject) 

and let the scene manager do your job. Without creating a scene in the scene: createScene () your ability to jump to the screen will not work. Without inserting things into a group, she cannot delete them for you when the scene leaves.

+1
source

Source: https://habr.com/ru/post/1487348/


All Articles