【问题标题】:Can I call a table inside itself in Lua?我可以在 Lua 中调用一个内部的表吗?
【发布时间】:2015-05-25 05:49:14
【问题描述】:

所以我正在尝试这个:

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}

}

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

有什么想法吗?

【问题讨论】:

    标签: lua call lua-table love2d


    【解决方案1】:

    你不能。

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

    【讨论】:

    • 谢谢。我会考虑到这一点:)
    猜你喜欢
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多