在 CoronaSDK 中生成和操作 table 个 "complex" 个对象

Generate and manipulate a table of "complex" objects in CoronaSDK

我正在尝试生成 40 个带文本的圆圈,我希望能够使用它们并在以后处理它们。

根据我在这里和 corona 论坛上发现的几个 post,我做得很好,但在所有示例中,只有一个 "display" 对象...而不是多个所以我不确定它是否可能或者它是否需要不同的东西。

经过一些介绍...这里是代码:

function _.spawn(params)
    local orb = display.newCircle( display.contentWidth/2, display.contentHeight/2, 40 ) -- creates the orb shape
    orb.orbTable = params.orbTable --assign the internal table
    orb.index = #orb.orbTable + 1 -- takes control of the index

    orb.orbTable[orb.index] = orb -- assign a orb to the internal table
    orb:setFillColor( unpack(params.color) ) -- change the color according to the rules above
    orb.x = params.x -- control the X position of the orb
    orb.y = params.y  --control the Y position of the orb
    orb.alpha = 1 -- borns with alpha = 0
    orb.value = params.value -- assign the internal value of the orb (1-10)

    --local orbText = display.newText( orb.value, orb.x+2, orb.y-5, "Aniron", 40 ) -- generates the value on the ball
    --orbText.orbTable = params.orbTable 
    --orbText.index = #orbText.orbTable + 1
    --orbText.orbTable[orbText.index] = orbText
    --orbText:setFillColor( 0,0,0 )
    --orbText.alpha = 1

    --The objects group
    orb.group = params.group or nil

    --If the function call has a parameter named group then insert it into the specified group
    orb.group:insert(orb)
    --orb.group:insert(orbText)

    --Insert the object into the table at the specified index
    orb.orbTable[orb.index] = orb
    return orb -- returns the orb to have control of it
end

生成我需要的所有对象的函数是:

function _.generateDeck()
    for i = 1, 40 do -- loop to generate the 40 orbs
        internalValue = internalValue + 1 -- counter to assigns the right values
        if (internalValue > 10) then -- if the internal value is more than 10 starts again (only 1-10)
            internalValue = 1 
        end
        if (i >= 1 and i <= 10) then -- assign RED color to the first 10 orbs
            completeDeck[i] = _.spawn({color = {196/255,138/255,105/255}, group = orbsGroup, x =  100, y = display.contentHeight / 2, value = internalValue, orbTable = completeDeck})
        elseif (i >= 11 and i <= 20) then -- assigns YELLOW color to the next 10 orbs
            completeDeck[i] = _.spawn({color = {229/255,214/255,142/255}, group = orbsGroup, x =  100, y = display.contentHeight / 2, value = internalValue, orbTable = completeDeck})
        elseif (i >= 11 and i <= 30) then -- assigns BLUE color to the next 10 orbs
            completeDeck[i] = _.spawn({color = {157/255,195/255,212/255}, group = orbsGroup, x =  100, y = display.contentHeight / 2, value = internalValue, orbTable = completeDeck})
        elseif (i >= 11 and i <= 40) then -- assigns GREEN color to the next 10 balls
            completeDeck[i] = _.spawn({color = {175/255,181/255,68/255}, group = orbsGroup, x =  100, y = display.contentHeight / 2, value = internalValue, orbTable = completeDeck})
        end
    end
end

调用函数后,它正确生成了圆圈,我可以读取值:

for i = 1, #completeDeck do
    print("Complete Deck Value ("..i.."): "..completeDeck[i].value)
end

但是,如果我取消注释有关 newText (orbText) 的行,那么我将无法读取这些值...(值为 nil 值)

我想我需要用我想要的对象(圆圈和文本)创建一个 displayGroup,然后 "spawn" 它,修改值?在那种情况下...我如何引用 displayGroup 中的对象?

我想我混合了不同的概念。

在 Lua 中,对象是 table,要创建一个由现有对象组成的对象,我们创建一个 table 并将对象作为值添加到其中。下面的代码创建了一个带有 orborbText 的对象,可以通过 object.orbobject.orbText

访问
function _.spawn(params)
    local orb = display.newCircle(display.contentWidth/2, display.contentHeight/2, 40) -- creates the orb shape
    orb:setFillColor(unpack(params.color))
    orb.x = params.x 
    orb.y = params.y
    orb.alpha = 1

    local orbText = display.newText(param.value, param.x + 2, param.y - 5, "Aniron", 40) 
    orbText:setFillColor(0, 0, 0)
    orbText.alpha = 1

    -- create an object with orb and orbText as values
    local object = {orb = orb, orbText = orbText}

    -- add more values to the object
    object.value = params.value
    object.index = #params.orbTable + 1
    params.orbTable[object.index] = object     

    return object
end

现在要使用对象:

for i, object in ipairs(completeDeck) do
     print(object.value)
end