How to spawn a set of objects in ascending order?

Beginner in CoronaSDK, just know how to use math.random to display the object. Want to learn how to spawn a set of 5 objects in ascending order.

local bubbles = {} local bubblesCounter = 0 local function tapBubbles (event) local obj = event.target if (event.phase == "began") then audio.play(popSound) display.remove( obj ) end end local function sendBubbles() local differentbubbles = {"images/bubbles/red1.png","images/bubbles/yellow1.png","images/bubbles/blue1.png", "images/bubbles/green1.png","images/bubbles/purple1.png"} local r = math.random( 1, #differentbubbles ) bubbles[bubblesCounter] = display.newImageRect( sceneGroup, differentbubbles[r], 82, 82 ) bubbles[bubblesCounter]:addEventListener( "touch", tapBubbles ) bubbles[bubblesCounter].x = math.random (42,278) bubbles[bubblesCounter].y = -40 physics.addBody( bubbles[bubblesCounter], "dynamic", {radius= 39}) transition.to( bubbles[bubblesCounter],{y =472, time=10000, onComplete=function(self) if (self~=nil) then display.remove(self) end end}) end timer.performWithDelay( 2000, sendBubbles, 5)

I think you need to add

bubblesCounter = bubblesCounter + 1 at the top of sendBubbles() so index increases by 1 each time a bubble is spawned.

Nail

Thank you, but what i need to do if i want to spawn the bubbles in ascending order? Means i want spawn 5 different colour bubbles in ascending order but don’t want in math.random

I think you need to add:

table.remove(differentbubbles, r)

this will remove the indexed bubble color from the table, leaving only the unused colors.

Use table.remove it you want to spawn each color in random order once.

if you want to spawn the bubbles in the table order, red being #1 then:

bubblesCounter = bubblesCounter + 1

bubbles[bubblesCounter] = display.newImageRect( sceneGroup, differentbubbles[bubblescounter], 82, 82 )

you won’t have to remove the item from the table since you are spawning in order

if you want to spawn the table in reverse, then do something like this:

local index = #differentbubbles - bubblesCounter

bubblesCounter = bubblesCounter + 1

bubbles[bubblesCounter] = display.newImageRect( sceneGroup, differentbubbles[index], 82, 82 )

Nail

Thanks. Just try the table.remove(differentbubbles, r) I have set the spawn bubbles to 5, but the first spawn bubbles was blue, second was red and then third was blue again, have any solution to make the 5 color bubbles spawn randomly in 5 times but the color wont repeat again? Here is my code, I not sure the table.remove (differentbubbles, r) put in the correct place.

local bubbles = {} local bubblesCounter = 0 local function sendBubbles() bubblesCounter = bubblesCounter + 1 local differentbubbles = {"images/bubbles/red1.png","images/bubbles/yellow1.png","images/bubbles/blue1.png","images/bubbles/green1.png","images/bubbles/purple1.png"} local r = math.random (1,#differentbubbles) bubbles[bubblesCounter] = display.newImageRect( sceneGroup, differentbubbles[r], 82, 82 ) table.remove(differentbubbles, r ) bubbles[bubblesCounter]:addEventListener( "touch", tapBubbles ) bubbles[bubblesCounter].x = math.random (42,278) bubbles[bubblesCounter].y = -40 physics.addBody( bubbles[bubblesCounter], "dynamic", {radius= 39}) transition.to( bubbles[bubblesCounter],{y =472, time=10000, onComplete=function(self) if (self~=nil) then display.remove(self) end end}) return bubbles[bubblesCounter] end timer.performWithDelay( 2000, sendBubbles, 5)

Sorry for broken english :slight_smile:

The table.remove() is in the correct place.

The problem is each time you call the sendBubbles() you are creating/spawning a new  differentbubbles{} with all 5 colors.

You need to move the table declaration,  differentbubbles{}, out of the sendBubbles(), place it above the function so table.remove removes the last spawned color and leaves the unused colors.

like this…

[lua]

local differentbubbles = {“images/bubbles/red1.png”,“images/bubbles/yellow1.png”,“images/bubbles/blue1.png”,“images/bubbles/green1.png”,“images/bubbles/purple1.png”}

sendBubbles()

–your code

end

[/lua]

Hope this helps

Thanks again, but one more thing i am confusing, if now i want the different bubbles spawn again after finish spawn the 5 color, what I need to do? Because if I put the differentbubbles{} outside of the function, the bubbles will just spawn 5 different color only. How to make it spawn the second, third set of differentbubbles{} after finish spawn the first set?

 There are probably many different ways to accomplish this, you could do something like this,

create a COPY of the differnetbubbles{}

I found the deep copy code below on stackoverflow, there are many variations.

The scheme is to remove the used colors from the Copied table (NewTable) and when the #NewTable == 0, spawn a new copy of the original differentbubbles{}.

[lua]

local bubbles = {}
local bubblesCounter = 0

local differentbubbles = {“images/bubbles/red1.png”,“images/bubbles/yellow1.png”,“images/bubbles/blue1.png”,“images/bubbles/green1.png”,“images/bubbles/purple1.png”}
local TempTable = {}

local function deepcopy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == ‘table’ then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[deepcopy(orig_key)] = deepcopy(orig_value)
        end
        setmetatable(copy, deepcopy(getmetatable(orig)))
    else – number, string, boolean, etc
        copy = orig
    end
    return copy
end

local function sendBubbles()
    bubblesCounter = bubblesCounter + 1

    print("#TempTable == ",#TempTable)
    
    if #TempTable == 0 then
        print(“create new TempTable”)
        TempTable = nil
        TempTable = deepcopy(differentbubbles)
    else
        print("else: #TempTable == ",#TempTable)
    end

    local  r = math.random (1,#TempTable)

   bubbles[bubblesCounter] = display.newImageRect( sceneGroup, TempTable[r], 82, 82 )

   table.remove(TempTable, r )

blah, blah

end

–change the timer to run infinitely

timer.performWithDelay( 1000, sendBubbles, -1)

[/lua]

Nail

Here is another scheme using a table of indexes, 1-5, instead of copying the original table.  This would be more efficient than using deepcopy.

Thought I would just show you different schemes.

[lua]

local bubbles = {}
local bubblesCounter = 0

local differentbubbles = {“images/bubbles/red1.png”,“images/bubbles/yellow1.png”,“images/bubbles/blue1.png”,“images/bubbles/green1.png”,“images/bubbles/purple1.png”}

local TempTable = {}

local function sendBubbles()
    bubblesCounter = bubblesCounter + 1

    print("#TempTable == ",#TempTable)

    if #TempTable == 0 then
        print(“create new TempTable”)
        TempTable = nil
        TempTable = {1, 2, 3, 4, 5}
    else
        print("else: #TempTable == ",#TempTable)
    end

    local  r = math.random (1,#TempTable)
    
    print("r == ",r)
    
   local NewBalloonIndex = TempTable[r]

  bubbles[bubblesCounter] = display.newImageRect( sceneGroup, differentbubbles[NewBalloonIndex], 82, 82 )

   table.remove(TempTable, r )

blah, blah

end

[/lua]

Nail

It is a list of great code to accomplish my task, thanks alot! :slight_smile:

I’m glad I could help, it was fun.

I hope you learned a few lessons on using tables to solve tasks.

Nail

For the code you solve for me, how about if i don’t want it run in the math.random. For example after TempTable = nil, TempTable2 = {2,3,4,5,1}, if TempTable2= nil, TempTable3 = {4,3,1,2,5}. Sorry, i try play around with the code but always show me error. 

hmmm…

I’d reference an indexed table of preset index values like this.  This scheme does not use table.remove.

Index references each tables 5 indexed values

Count references each original indexed table  …  Count = 1, Table[Count] == Table[1], etc

[lua]

local bubbles = {}
local bubblesCounter = 0

local TempTable = {}

local Table = {}   --declare the 3 indexed tables of set spawn orders
Table[1]= {1,2,3,4,5}
Table[2] = {2,3,4,5,1}
Table[3] = {4,3,1,2,5}

TempTable[1] = Table[1]

local Count = 1
local Index = 0

local differentbubbles = {“images/bubbles/red1.png”,“images/bubbles/yellow1.png”,“images/bubbles/blue1.png”,“images/bubbles/green1.png”,“images/bubbles/purple1.png”}

local function sendBubbles()

    bubblesCounter = bubblesCounter + 1
   Index = Index + 1
    
    print("#TempTable == ",#TempTable)
    print("Index == ",Index)
   
    if Index > 5 then  – check that 5 indexed values of each Table have been spawned
        
        print(“create new Indexed TempTable”)
        
        Count = Count + 1
        Index = 1
        
        if Count < 4 then   --check to see if the Table[3] has been spawned
            
            TempTable[Count] = Table[Count]
            Index = 1
        else    --reset and start over with Table[1]
            Index = 1
            Count = 1
            TempTable = nil
            TempTable = {}
           
            TempTable[Count] = Table[Count]
 
        end
    else   
    end
  
    local NewBalloonIndex = TempTable[Count][Index]
    
     bubbles[bubblesCounter] = display.newImageRect( sceneGroup, differentbubbles[NewBallowIndex], 82, 82 )

blah, blah

end

[/lua]

Does this work?

Nail

Hi, i will try this code after my exam, currently preparing for college exam. May you give me some suggestion how to improve in CoronaSDK coding skill? I am new to programming and just have 2 month coding experience, feel always get a lot error in my code. Do I need to buy some CoronaSDK Game Development books?

The codes works well, Thanks ~ :). Another issue that I have meet is when I add the second line of timer.performWithDelay, the system show like this

Copyright (C) 2009-2016 C o r o n a L a b s I n c . Version: 3.0.0 Build: 2016.2830 Platform: iPhone / x86\_64 / 10.11.3 / Intel Iris OpenGL Engine / 2.1 INTEL-10.12.13 / 2016.2830 / en | US | en\_US | en Loading project from: ~/Desktop/BubblePop Project sandbox folder: ~/Library/Application Support/Corona Simulator/BubblePop-2F7C7498DCF38E8DCE5E215B46E4CA47 #TempTable == 1 Index == 1 #TempTable == 1 Index == 2 #TempTable == 1 Index == 3 #TempTable == 1 Index == 4 #TempTable == 1 Index == 5 #TempTable == 1 Index == 6 create new Indexed TempTable #TempTable == 2 Index == 2 #TempTable == 2 Index == 3 #TempTable == 2 Index == 4 #TempTable == 2 Index == 5 #TempTable == 2 Index == 6 create new Indexed TempTable #TempTable == 3 Index == 2 #TempTable == 3 Index == 3 #TempTable == 3 Index == 4 #TempTable == 3 Index == 5 #TempTable == 3 Index == 6 create new Indexed TempTable 2016-07-15 02:58:58.524 Corona Simulator[3037:634274] Corona Simulator: Goodbye [Finished in 18.4s]

First table work normally with Index 1 to Index 6, but when comes to Table 2, It will start with Index 2. What action i need to take in order to make the second table begin with Index 1?

hmmm…

Are you sure all 5 colors are not spawning for Table2 and Table3, or are you just reading the print log?

If you add a print just above  the NewBalloonIndex  declaration…

print("Index == "Index)

local NewBalloonIndex = TempTable[Count][Index]

I think you will see Index == 1 print after create new Indexed TempTable prints.

Nail

As far as learning to write code, I have 3 suggestions.

1.  Play with as many examples and tutorials of corona apps you can find until you understand what is happening.

I haven’t read any Corona / lua books, but I’m sure they would be beneficial. 

2.  Play with tables using print statements until you understand how they work and what they can do for you.

3,  Remember, Google is you friend.

Nail

I think i have make some mistake, because i make the bubbles spawn at (top y-axis  -200) of the screen and make some bubbles run out of the screen since i got add physics body, now i add left and right wall then every problems is solved :slight_smile:

Yea, Google is always our friend, but mostly i visit 10 website to understand the example of the code, I not understand all of the concept. I think because I am still new to Corona. Can you suggest me any website to get the tutorial? Currently I am just focus on Udemy course only.

try this table…

local differentbubbles = {}
    differentbubbles[1] = {}
    differentbubbles[1].Image = “images/bubbles/red1.png”
    differentbubbles[1].Value = 10
    
    differentbubbles[2] = {}
    differentbubbles[2].Image = “images/bubbles/yellow1.png”
    differentbubbles[2].Value = 20
    
    differentbubbles[3] = {}
    differentbubbles[3].Image = “images/bubbles/blue1.png”
    differentbubbles[3].Value = 30
    
    differentbubbles[4] = {}
    differentbubbles[4].Image = “images/bubbles/green1.png”
    differentbubbles[4].Value = 40
    
    differentbubbles[5] = {}
    differentbubbles[5].Image = “images/bubbles/purple1.png”
    differentbubbles[5].Value = 50

–add local Score = 0  near the top of you code

– in you tapBubbles() add, Score = Score + event.target.Value

   --print("Score == ",Score)

– add this property to your bubbles when they spawn,  bubbles[bubblesCounter].Value  = differentbubbles[NewBalloonIndex].Value

Now when you “pop” a bubble, you increase the score by the bubbles Value

Nail