[英]Corona SDK PurgeScene destroys physics
I have create a simple scrolling game, following closely to Mark Farkland's Game tutorial. 我紧接着Mark Farkland的游戏教程,创建了一个简单的滚动游戏。 I clearly followed his coding pattern but got to the point where I am just lost and don't why mine behaves different that his example. 我清楚地遵循了他的编码方式,但是到了我迷路的地步,不知道为什么我的举止与他的例子不同。 Everything is working fine until after a scene is purged. 在清除场景之前,一切正常。
I have two lua files: game.lua and gameover.lua. 我有两个lua文件:game.lua和gameover.lua。 In game.lua exitScene I have removed all the eventlisteners. 在game.lua exitScene中,我删除了所有事件监听器。 In gameover.lua enterScene, I have purged game.lua scene. 在gameover.lua中进入场景,我清除了game.lua场景。 When the user touched the screen it then loads game.lua file. 当用户触摸屏幕时,它将加载game.lua文件。 Every objects load and refreshes like new apart from the main object which just shoots upwards. 除了向上射击的主要对象外,每个对象都像新对象一样加载和刷新。
Game.lua Game.lua
local storyboard = require("storyboard")
local scene = storyboard.newScene()
local physics = require("physics")
function scene:createScene(event)
physics.start()
....
local jet = display.newImage("images/jet_normal.png")
physics.addBody(jet, "dynamic", {density=.07, bounce=0.1, friction=.2, radius=12})
local activateJets = function(self, event)
self:applyForce(0, -1.5, self.x, self.y)
end
function touchScreen(event)
local phase = event.phase
if phase == "began" then
jet.enterFrame = activateJets
Runtime:addEventListener("enterFrame", jet)
end
if phase == "ended" then
Runtime:removeEventListener("enterFrame", jet)
end
end
Runtime:addEventListener("touch", touchScreen)
end
function scene:exitScene(event)
Runtime:removeEventListener("touch", touchScreen)
Runtime:removeEventListener("enterFrame", enemy1)
Runtime:removeEventListener("collision", onColission)
end
GameOver.lua GameOver.lua
function start(event)
if event.phase == "began" then
storyboard.gotoScene("game", "fade", 100)
end
end
function scene:enterScene(event)
storyboard.purgeAll("game")
background:addEventListener("touch", start)
end
function scene:exitScene(event)
background:removeEventListener("touch", start)
end
You need to add the jet to the current group. 您需要将喷气机添加到当前组。
function scene:createScene(event)
local group = self.view
--...
local jet = display.newImage("images/jet_normal.png")
--...
group:insert(jet)
Runtime:addEventListener("touch", touchScreen)
end
The proper cleanup will happen to the group when exitScene is triggered. 触发exitScene时,将对组进行适当的清理。
more info / different implementation: 更多信息/不同的实现:
http://docs.coronalabs.com/guide/graphics/group.html http://docs.coronalabs.com/guide/graphics/group.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.