Search code examples
luamoai

How to iterate on a dictionary/table of textboxes?


I have a table of textboxes, where the key is the name of the position (topRight, topLeft...).

Creating and configuring one at a time works fine:

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

But not when I try iterating through it:

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

What I'm doing wrong?


Solution

  • Use pairs() instead of ipairs().