【问题标题】:Generate and manipulate a table of "complex" objects in CoronaSDK在 CoronaSDK 中生成和操作“复杂”对象表
【发布时间】:2015-11-25 03:01:29
【问题描述】:

我正在尝试生成 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) 的行,那么我将无法读取这些值...(值是 nil 值)

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

我认为我在混合不同的概念。

【问题讨论】:

  • 在注释掉的代码中,orbText 被添加到 completeDeckorbText 没有 value 字段。您希望最终结果是什么?
  • 感谢您的回复,正如我所说...我是这方面的新手...我想要一个里面有两个对象的对象,文本和圆圈等等属性,并能够单独访问和处理它们。我不知道我是否解释得很好......像object[index].orb.x或object[index].value或object[index].orbText.text,......

标签: lua coronasdk lua-table


【解决方案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

【讨论】:

  • 非常感谢!!!就是我要找的!!!抱歉新手问题!!!超级,超级感谢:)
猜你喜欢
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多