4

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

游戏.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

问候凯文,

4

3 回答 3

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/

于 2013-06-21T01:13:23.443 回答
1

既然您正在使用情节提要,为什么不简单地将所有显示对象插入组中:

 group:insert(someobject)

让场景管理器为您完成工作。通过不在 scene:createScene() 中创建场景,您无法转换屏幕。通过不将东西放入组中,当场景消失时它无法为您移除它们。

于 2013-07-01T01:00:48.827 回答
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()进入虚拟场景时删除所有侦听器并将其放在函数上。

于 2013-06-21T02:01:27.883 回答