调用自我功能的错误?

时间:2018-03-21 18:10:05

标签: lua love2d

我写了这段代码:

Editor = {
  x = 10,
  y = 10,
  interpreter = nil,
  cursor = nil
}

-- Some code here

function Editor:drawValues(w, h)
  for x = 0, w - 1 do
    for y = 0, h - 1 do
      local _x = self.x + x * CELL_SIZE
      local _y = self.y + y * CELL_SIZE
      love.graphics.print(self.interpreter.grid.cell[x][y], _x, _y) -- ERROR THIS!
    end
  end
end

function Editor:draw()
  local w = self.interpreter.grid.width
  local h = self.interpreter.grid.height

  Editor:drawGrid(w, h)
  Editor:drawValues(w, h)

  -- Its Works VVVVVVV
  -- for x = 0, w - 1 do
  --   for y = 0, h - 1 do
  --     local _x = self.x + x * CELL_SIZE
  --     local _y = self.y + y * CELL_SIZE
  --     local value = self.interpreter.grid.cell[x][y]
  --     love.graphics.print(value, _x, _y) -- self.interpreter.grid.cell[x][y]
  --   end
  -- end
end

return Editor

函数Editor:draw调用Editor:drawGrid并且它有效,但调用Editor:drawValues(w, h)会引发错误:

  

尝试索引字段'interpreter'(零值)

但是!如果我发表评论Editor:drawValues(w, h)并取消注释以下代码,则可行。为什么呢?

为我的英语道歉。

0 个答案:

没有答案