【问题标题】:How to iterate on a dictionary/table of textboxes?如何迭代文本框的字典/表?
【发布时间】:2012-11-27 21:59:42
【问题描述】:

我有一个文本框表,其中的键是位置的名称(topRight、topLeft...)。

一次创建和配置一个工作正常:

kanaAt = {}
function startKanas ()
    kanaAt.topLeft = MOAITextBox.new()  
    kanaAt.topLeft:setFont(font)
    ...
    kanaAt.topLeft:setString("か")
    layer:insertProp (kanaAt.topLeft)

    kanaAt.topRight = MOAITextBox.new()
    kanaAt.topRight:setFont(font)
    ...
    kanaAt.topRight:setString("た")
    layer:insertProp (kanaAt.topRight)
end

但当我尝试遍历它时却不是:

kanaAt = {}
function startKanas ()
    kanaAt.topLeft = MOAITextBox.new()
    kanaAt.topRight = MOAITextBox.new() 
    kanaAt.bottomLeft = MOAITextBox.new()
    kanaAt.bottomRight = MOAITextBox.new()          

    for name, text in ipairs(kanaAt) do
        text:setFont(font)
        text:setTextSize(90,60)
        text:setYFlip(true)
        text:setRect(-50,-50,50,50)
        layer:insertProp (text)
    end

    kanaAt.topLeft:setString("か")
    kanaAt.topLeft:setLoc(-325, 225)
    kanaAt.topRight:setString("た")
    kanaAt.topRight:setLoc(325, 225)
    kanaAt.bottomLeft:setString("ち")
    kanaAt.bottomLeft:setLoc(-325, -225)
    kanaAt.bottomRight:setString("つ")
    kanaAt.bottomRight:setLoc(325, -225)

end

我做错了什么?

【问题讨论】:

    标签: lua moai


    【解决方案1】:

    使用pairs() 代替ipairs()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2019-01-12
      相关资源
      最近更新 更多