Juanjo García
Juanjo García

Reputation: 67

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

I'm trying to generate 40 circles with text and I want to be able to use them and handle them later.

According to several post I found here and on corona forums, I'm doing it well but, in all the examples, is only with one "display" object... not with several so I'm not sure if it's possible or maybe it needs something different.

After some introduction... here the code:

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

The function to generate all the objects I need is:

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

After calling the function, it generates the circles correctly and I can read the value:

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

But if I uncomment the lines regarding the newText (orbText) then I'm not able to read the values... (value is a nil value)

I guess I need to create a displayGroup with the objects I want inside (circle and text), then "spawn" it, modifying the values? In that case... how can I refer to the object inside the displayGroup?

I think I'm mixing different concepts.

Upvotes: 2

Views: 197

Answers (1)

ryanpattison
ryanpattison

Reputation: 6251

In Lua, objects are tables and to create an object composed of existing objects, we create a table with and add the objects to it as values. The code below creates an objects with orb and orbText which can be accessed by object.orb or object.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

Now to use the object:

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

Upvotes: 1

Related Questions