我可以在Lua里面调用一张桌子吗?

时间:2015-03-22 01:15:27

标签: lua call lua-table love2d

所以我正在尝试这个:

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}

}

我目前收到此错误:     尝试索引全局“按钮”(零值)

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

你不能。

在计算表构造函数之前,不会创建表。所以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