如何清除’game.lua中的所有对象,当我离开游戏场景时,所有数据仍然在屏幕上,当我离开时如何移除所有内容并在我返回时将其重置回起始位置game.lua’? game.lua: local storyboard = require(
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
关心凯文,
如果您正在使用故事板并希望重置所有值,则可以在转到另一个场景之前删除场景,这样当您再次进入同一场景时,将创建所有值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
或者在你去现场后这样做
function scene:enterScene( event ) storyboard.removeScene("SCENE TO REMOVE") end
这是关于如何实现它的链接.
http://www.coronalabs.com/blog/2012/07/31/storyboard-scene-purging-vs-removal/