清理数据表单场景

时间:2013-06-20 18:05:14

标签: lua corona corona-storyboard

如何清除'game.lua中的所有对象,当我离开游戏场景时,所有数据仍然在屏幕上,当我离开时如何移除所有内容并在我离开时将其重置回起始位置回到'game.lua'?

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

问候凯文,

3 个答案:

答案 0 :(得分:2)

如果你正在使用故事板并希望重置所有值,你可以在去另一个场景之前移除场景,所以当你再次去同一个场景时,所有的都将被创建

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/

答案 1 :(得分:1)

有两种方法可以重置对象位置和可变数据

第一个一个是创建一个像这样的函数

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

每当您需要重置游戏时调用它,但您将手动编码重置值。

第二一个是创建一个虚拟场景。 虚拟场景是一个将您重新定向到游戏场景的场景,如此

虚拟场景

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

重置对象和变量的值,但是当你转到虚拟场景时,不要忘记删除所有的侦听器并将其放在exitScene()函数上。

答案 2 :(得分:1)

由于您使用的是故事板,为什么不简单地将所有显示对象插入到组中:

 group:insert(someobject)

让场景经理为你工作。通过不在场景中创建场景:createScene()您转换屏幕的能力将无法正常工作。通过不将内容放入组中,当场景消失时,它无法为您删除它们。