使用字段名称定义函数时出错(`'('应在 '['` 附近)

mDe*_*ram 2 lua love2d

我在用lua编译时出错:

local action = {}
action["moving"] = {}
action["zooming"] = {}
action["moving"].state = false
action["zooming"].state = false

action.list = {"moving", "zooming"}

function action["moving"].f()
  if CanAction("moving") and (love.keyboard.isDown("left") or love.keyboard.isDown("right") or love.mouse.isDown("l")) then
    if action["moving"].state == false then
      action["moving"].x, action["moving"].y = camera:mousePosition()
      action["moving"].state = true
    end
    if action["moving"].state then
      if love.mouse.isDown("l") then
        camera:setPosition(-love.mouse.getX()*camera.scaleX + action["moving"].x, -love.mouse.getY()* camera.scaleY + action["moving"].y)
      elseif love.keyboard.isDown("left") then
        camera:move(-10*camera.scaleX, 0)
      elseif love.keyboard.isDown("right") then
        camera:move(10*camera.scaleX, 0)
      end
    end
  else
    ResetAction("moving")
  end
end 
Run Code Online (Sandbox Code Playgroud)

当我声明函数“function action[”moving”].f()”时,错误是关于那一行的,也许是因为我使用了这样的表,谢谢你的帮助。

Pau*_*nko 5

您应该使用action["moving"].f = function()代替,function action["moving"].f()因为后一种语法是不允许的,而前者会为表中的字段分配一个匿名函数。