Quick Syntax question

Hey guys got a quick syntax question. So lets say i create 4 game objects like so…

[lua]local goblin_1 = display.newImageRect( “goblin.png”, 50, 50 )
goblin_1.x = 734
goblin_1.y = 255
physics.addBody( goblin_1, { density=1, friction=0.3, bounce=0.2 } )

local goblin_2 = display.newImageRect( “goblin.png”, 50, 50 )
goblin_2.x = 1054
goblin_2.y = 255
physics.addBody( goblin_2, { density=1, friction=0.3, bounce=0.2 } )

local goblin_3 = display.newImageRect( “goblin.png”, 50, 50 )
goblin_3.x = 1072
goblin_3.y = 205
physics.addBody( goblin_3, { density=1, friction=0.3, bounce=0.2 } )

local goblin_4 = display.newImageRect( “goblin.png”, 50, 50 )
goblin_4.x = 1106
goblin_4.y = 254
physics.addBody( goblin_4, { density=1, friction=0.3, bounce=0.2 } )[/lua]

now i want to insert all 4 of these objects into a display group, normally i would do it by hand, im trying to figure out how to use a FOR loop to do so… what i have right now is

[lua]for i = 1, 4 do
M.masterBlockGroup:insert(goblin_i)
end[/lua]

What would be the correct syntax to accomplish this? thanks!
[import]uid: 19620 topic_id: 21882 reply_id: 321882[/import]

You’d want them already in a table, something like this;

[lua]localGroup = display.newGroup()

local goblin = {}

for i =1, 4 do
goblin[i] = display.newCircle(100, i*100, 20)
localGroup:insert( goblin[i] )
end[/lua]

That’s circles and I’m not adding physics, etc, but you get the idea I hope.

Peach :slight_smile: [import]uid: 52491 topic_id: 21882 reply_id: 86999[/import]

What Peach said!

In addition you are going to still have to do your X, Y coordinates, but you might want to try this:

local locations = {}  
locations[1] = {x=734, y=255}  
locations[2] = {x=1054, y=255}  
locations[3] = {x=1072, y=205}  
locations[4] = {x=1106, y=254}  
  
localGroup = display.newGroup()  
   
local goblin = {}  
   
for i =1, 4 do  
 goblin[i] = display.newCircle(100, i\*100, 20)  
 goblin[i].x = locations[i].x  
 goblin[i].y = locations[i].y  
 localGroup:insert( goblin[i] )  
end  

or something like that. [import]uid: 19626 topic_id: 21882 reply_id: 87034[/import]

Thanks so much guys, time and time again i keep needing to fall back on tables, they are so useful, i just need to make a habit of using them. Thank you for this info, helps me understand how i can use tables in this situation. [import]uid: 19620 topic_id: 21882 reply_id: 87035[/import]

Thanks Rob - I should have included that myself but was in a rush this morning, very valuable info.

Good luck with your project rxmarccall - tables are indeed very useful :slight_smile:

Peach [import]uid: 52491 topic_id: 21882 reply_id: 87058[/import]

That’s why we make such an awesome team! [import]uid: 19626 topic_id: 21882 reply_id: 87091[/import]

+1! [import]uid: 52491 topic_id: 21882 reply_id: 87239[/import]