我创建了一个简单的滚动游戏,紧跟 Mark Farkland 的游戏教程。我清楚地遵循了他的编码模式,但到了我迷路的地步,我不明白为什么我的行为与他的例子不同。在清除场景之前一切正常。
我有两个 lua 文件:game.lua 和 gameover.lua。在 game.lua exitScene 中,我删除了所有事件监听器。在 gameover.lua enterScene 中,我已经清除了 game.lua 场景。当用户触摸屏幕时,它会加载 game.lua 文件。除了刚刚向上射击的主要对象之外,每个对象都像新对象一样加载和刷新。
游戏.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
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