我正在使用 Coronasdk 创建一个新游戏,我需要让树从屏幕右侧出现并让它们向左滚动。那是代码
local Trees = {} --I generate trees that appears randomly
local function createTree(event)
table.insert(Trees, tree)
return tree
end
local function treeScroll (self, event)
self.x = self.x - 3
end
MIN = 3000
MAX = 7000
local function gameLoop()
local tree = createTree()
tree.enterFrame = treeScroll
Runtime:addEventListener("enterFrame", tree)
for i, thisTree in ipairs (Trees) do
if thisTree.x < -100 then
Runtime:removeEventListener("enterFrame", thisTree)
display.remove (thisTree)
table.remove (Trees, i)
end
end
timer.cancel(timer1)
timer1=timer.performWithDelay(math.random(MIN, MAX), gameLoop, 0)
end
timer1 = timer.performWithDelay (math.random(MIN, MAX), gameLoop, 0)
它告诉我 addEventListener:Listener 不能为 nil:nil,它指的是这部分
local function gameLoop()
local tree = createTree()
tree.enterFrame = treeScroll
Runtime:addEventListener("enterFrame", tree)
for i, thisTree in ipairs (Trees) do
if thisTree.x < -100 then
Runtime:removeEventListener("enterFrame", thisTree)
display.remove (thisTree)
table.remove (Trees, i)
end
end
timer.cancel(timer1)
timer1=timer.performWithDelay(math.random(MIN, MAX), gameLoop, 0)
end
我不知道该怎么办