addEventListener: listener 不能为 nil:nil

我正在使用 Coronasdk 制作一个新游戏,我需要从屏幕右侧生成树并使它们向左滚动。 这是代码

      local Trees = {}  --我随机生成树
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

我不知道该怎么办

原文链接 https://stackoverflow.com/questions/71110587

点赞