尝试索引局部变量

我遇到了错误:

libs/timer.lua:144: attempt to index local 'subject' (a nil value)

Traceback

libs/timer.lua:144: in function 'tween_collect_payload' libs/timer.lua:158: in function 'tween' classes/Button.lua:64: in function 'listenKey' states/menu.lua:35: in function 'keypressed' main.lua:58: in function <main.lua:57> [C]: in function 'xpcall'

我认为有问题,但我不知道具体在哪里:

classes/Button.lua

function Button:listenKey(key)
  local action = self.buttons[self.currentPosition][2]
  local prev = self.currentPosition - 1
  local nex = self.currentPosition + 1
  moveRight = function(buttonPos)
    timer.tween(.5, buttonPos, {[3] = 150}, "in-out-linear")
  end
  if key == "w" or key == "up" then
    sounds.menu.move:stop()
    sounds.menu.move:play()
    if self.currentPosition > 1 then
      -- 这里出现了错误
      timer.tween(.5, self.buttons[nex], {[3] = 150}, "in-out-linear")
    end
    moveRight(self.buttons[self.currentPosition])
    self.currentPosition = math.max(1, self.currentPosition - 1)
  elseif key == "s" or key == "down" then
    sounds.menu.move:stop()
    sounds.menu.move:play()
    if self.currentPosition <= lume.count(self.buttons) then
      -- 还有这里出现了错误
      timer.tween(.5, self.buttons[prev], {[3] = 150}, "in-out-linear")
    end
    moveRight(self.buttons[self.currentPosition])
    self.currentPosition = math.min(lume.count(self.buttons), self.currentPosition + 1)
  elseif key == "return" then
    sounds.menu.move:stop()
    sounds.menu.enter:stop()
    sounds.menu.enter:play()
    action()
  end
end

使用了 hump.timer 和 middleclass 库。 尝试了很多获取下一个和上一个位置的变化,但无论如何我都无法以正确的 "lua-way" 获取到它

点赞