在CoronaSDK中生成并操作“复杂”对象表

时间:2015-08-31 06:28:16

标签: lua corona lua-table

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

根据我在这里和电晕论坛上发现的几篇文章,我做得很好但是,在所有的例子中,只有一个“显示”对象...没有几个所以我不确定它是否是可能或者它需要不同的东西。

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

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)的行,那么我就无法读取值...(值是零值)

我想我需要用我想要的对象(圆圈和文本)创建一个displayGroup,然后“生成”它,修改值?在那种情况下......我怎样才能引用displayGroup中的对象?

我想我正在混合不同的概念。

1 个答案:

答案 0 :(得分:1)

在Lua中,对象是表,并且要创建由现有对象组成的对象,我们创建一个表,并将对象作为值添加到其中。下面的代码创建了一个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