我可以在Lua中调用表格本身吗?

所以我正在尝试这样做:

buttons = {

{imageNothing = love.graphics.newImage("buildingButtonNotSelected.png"), imageHovering = love.graphics.newImage("buildingButtonHovering.png"), imageSelected = love.graphics.newImage("buildingButton.png"),imgW = buttons[1].imageNothing:getWidth(), imgH = buttons[1].imageNothing:getHeight(), imgX = windowWidth - buttons[1].imgW, imgY = windowHeight - buttons[1].imgH, selected = false, hovering = false}

}

我目前得到了这个错误: 尝试索引全局'buttons' (空值)

有什么想法吗?

点赞
用户1009479
用户1009479

你不能这样做。

直到表构造函数被评估之前,表才被创建。因此,在表构造函数内,buttons还未定义。

您可以在表构造函数内部不使用 `buttons 初始化 buttons,然后稍后添加这些字段。

buttons = {
  {
    imageNothing = love.graphics.newImage("buildingButtonNotSelected.png"),
    imageHovering = love.graphics.newImage("buildingButtonHovering.png"),
    imageSelected = love.graphics.newImage("buildingButton.png"),
    selected = false,
    hovering = false
  }
}

buttons.imgW = buttons[1].imageNothing:getWidth()
buttons.imgH = buttons[1].imageNothing:getHeight()
buttons.imgX = windowWidth - buttons[1].imgW
buttons.imgY = windowHeight - buttons[1].imgH
2015-03-22 02:30:26