adding images within a table to localGroup, how?

Iam having an issue where Iam trying to add 4 images from a table into local group.

local images = {}  
  
 images[#images + 1] = "images/leafs.png"  
 images[#images + 1] = "images/leaf2.png"  
 images[#images + 1] = "images/leaf3.png"  
 images[#images + 1] = "images/leaf4.png"  

As you can see, I am trying to reference each image object , so how do I do this please?
any suggestions appreciated.

[import]uid: 127675 topic_id: 31049 reply_id: 331049[/import]

When you finished your image table code just loop through them to add them to a the desired group. Example:

[lua]for i, line in ipairs(images) do
myLocalGroup:insert(images[i])
end[/lua]

[import]uid: 147305 topic_id: 31049 reply_id: 124139[/import]

@budershank,
thanks for the help, I have looked at the syntax for ipairs but I dont really understand the “line” in the code.
reference youre making ?
could you be a little more clearer for someone relatively fresh to this please?
[import]uid: 127675 topic_id: 31049 reply_id: 124155[/import]

No. Not being a jerk I just never bothered looking into that loop method aside from iterating through a table. I suppose the line in ipairs(table) is just a fancy way of getting the number of entries in the table. “for i = 1, table.getn(myTable) do” would give the same result, really. [import]uid: 147305 topic_id: 31049 reply_id: 124156[/import]

The function ‘ipairs’ is a Lua method for iterating through a table with numerical indices. Consider the following table:

[lua]myTable = {}
table.insert(myTable, #myTable + 1, “some value”)
table.insert(myTable, #myTable + 1, “another value”)
– This table can be accessed by number
print(myTable[1]) --prints “some value”
print(myTable[2]) --prints “another value”[/lua]

That table has numbers for indexes. If you created another table with keys instead of indexes, it would be necessary to use the ‘pairs’ function instead.
[lua]myTable = {}
myTable[“width”] = 50
print(myTable[1]) --prints ‘nil’
print(myTable[“width”]) --prints 50

–Indexes cannot be strings, they would be keys instead
myTable[“1”] = “some value”
myTable[1] = “second value”
print(myTable[1] == myTable[“1”]) --prints ‘false’

–Compare the output of the following two statements to see the difference
for key, value in pairs(myTable) do print(‘used pairs here’, key, value) end
for i, value in ipairs(myTable) do print(‘used ipairs here’, i, value) end[/lua]

In your previous example, ipairs was used to iterate through the numeric indices of the table and the variable ‘line’ refers to the value in the table at each index i. The ‘line’ variable could be used instead of referencing by [i]. Be careful, the string you are providing as the path to an image is not a Corona display object so it cannot be inserted into a display group. Try something like this instead:
[lua]for i, line in ipairs(images) do
local myImage = display.newImage(line)
myImage.x, myImage.y = display.contentCenterX, display.contentCenterY
myLocalGroup:insert(myImage)
end[/lua] [import]uid: 168249 topic_id: 31049 reply_id: 124168[/import]

Wow,
thanks for that cooperlabs.
I tried messing but still am unable to get them into the localGroup.
here is the whole table code, it is used to spawn 4 different leaves randomly.

local images = {}  
  
 images[#images + 1] = "images/leafs.png"  
 images[#images + 1] = "images/leaf2.png"  
 images[#images + 1] = "images/leaf3.png"  
 images[#images + 1] = "images/leaf4.png"  
  
  
 local function leafSpawn()  
 math.randomseed( os.time() )  
 local index = math.random( 1, #images )  
  
 local leaf = display.newImage( images[index] )  
 ----localGroup:insert(leaf)-----dosnt do anything no removal or error ----  
 physics.addBody(leaf, "dynamic",{radius=16, filter=leafCollisionFilter})  
 leaf.myName = "leaf"  
 leaf:addEventListener("collision", leaf)  
 leaf.x = -100  
 leaf.y = math.random(20,250)  
 --transition.from(leaf, {time = math.random(2400,4500), delay = 0, x = leaf.x +580,})  
 transition.from(leaf, {time = math.random(2400,4500), delay = 0, x = leaf.x +580, rotation = math.random (720)})  
  

I am obviously missing something simple, but be assured once I understand how to do it I never forget it.
Thanks again [import]uid: 127675 topic_id: 31049 reply_id: 124174[/import]

You don’t show where you defined your display group or called your leafSpawn function. Also, you may find it easier with lots of images to use string concatenation (those ‘…’ double dots) in a loop. Something like this should work.

[lua]local leafGroup = display.newGroup()
local images = {}
for i=1, 4 do
images[i] = “images/leaf”…i…".png"
end

local function leafSpawn()
math.randomseed( os.time())
local index = math.random(1, #images)
local leaf = display.newImage(images[index])
leaf.x, leaf.y = 100, 100
leafGroup:insert(leaf)
end

leafSpawn() --Loop this call if needed[/lua]

This code works to get your images in the local group though you may have to rename your first one from ‘leafs.png’ to ‘leaf1.png’. [import]uid: 168249 topic_id: 31049 reply_id: 124177[/import]

@cooperlabs,

hey, thanks for that.
I forgot to add the spawn function and display group. Yes this seems a much way of doing this.
And I think it works, hard to say as yet because I having real trouble cancelling timers and runtimes.
Have a look and comment if you like hint hint.

Thanks for replying it really does help us new developers to move on and understand better practices. [import]uid: 127675 topic_id: 31049 reply_id: 124336[/import]

When you finished your image table code just loop through them to add them to a the desired group. Example:

[lua]for i, line in ipairs(images) do
myLocalGroup:insert(images[i])
end[/lua]

[import]uid: 147305 topic_id: 31049 reply_id: 124139[/import]

@budershank,
thanks for the help, I have looked at the syntax for ipairs but I dont really understand the “line” in the code.
reference youre making ?
could you be a little more clearer for someone relatively fresh to this please?
[import]uid: 127675 topic_id: 31049 reply_id: 124155[/import]

No. Not being a jerk I just never bothered looking into that loop method aside from iterating through a table. I suppose the line in ipairs(table) is just a fancy way of getting the number of entries in the table. “for i = 1, table.getn(myTable) do” would give the same result, really. [import]uid: 147305 topic_id: 31049 reply_id: 124156[/import]

The function ‘ipairs’ is a Lua method for iterating through a table with numerical indices. Consider the following table:

[lua]myTable = {}
table.insert(myTable, #myTable + 1, “some value”)
table.insert(myTable, #myTable + 1, “another value”)
– This table can be accessed by number
print(myTable[1]) --prints “some value”
print(myTable[2]) --prints “another value”[/lua]

That table has numbers for indexes. If you created another table with keys instead of indexes, it would be necessary to use the ‘pairs’ function instead.
[lua]myTable = {}
myTable[“width”] = 50
print(myTable[1]) --prints ‘nil’
print(myTable[“width”]) --prints 50

–Indexes cannot be strings, they would be keys instead
myTable[“1”] = “some value”
myTable[1] = “second value”
print(myTable[1] == myTable[“1”]) --prints ‘false’

–Compare the output of the following two statements to see the difference
for key, value in pairs(myTable) do print(‘used pairs here’, key, value) end
for i, value in ipairs(myTable) do print(‘used ipairs here’, i, value) end[/lua]

In your previous example, ipairs was used to iterate through the numeric indices of the table and the variable ‘line’ refers to the value in the table at each index i. The ‘line’ variable could be used instead of referencing by [i]. Be careful, the string you are providing as the path to an image is not a Corona display object so it cannot be inserted into a display group. Try something like this instead:
[lua]for i, line in ipairs(images) do
local myImage = display.newImage(line)
myImage.x, myImage.y = display.contentCenterX, display.contentCenterY
myLocalGroup:insert(myImage)
end[/lua] [import]uid: 168249 topic_id: 31049 reply_id: 124168[/import]

Wow,
thanks for that cooperlabs.
I tried messing but still am unable to get them into the localGroup.
here is the whole table code, it is used to spawn 4 different leaves randomly.

local images = {}  
  
 images[#images + 1] = "images/leafs.png"  
 images[#images + 1] = "images/leaf2.png"  
 images[#images + 1] = "images/leaf3.png"  
 images[#images + 1] = "images/leaf4.png"  
  
  
 local function leafSpawn()  
 math.randomseed( os.time() )  
 local index = math.random( 1, #images )  
  
 local leaf = display.newImage( images[index] )  
 ----localGroup:insert(leaf)-----dosnt do anything no removal or error ----  
 physics.addBody(leaf, "dynamic",{radius=16, filter=leafCollisionFilter})  
 leaf.myName = "leaf"  
 leaf:addEventListener("collision", leaf)  
 leaf.x = -100  
 leaf.y = math.random(20,250)  
 --transition.from(leaf, {time = math.random(2400,4500), delay = 0, x = leaf.x +580,})  
 transition.from(leaf, {time = math.random(2400,4500), delay = 0, x = leaf.x +580, rotation = math.random (720)})  
  

I am obviously missing something simple, but be assured once I understand how to do it I never forget it.
Thanks again [import]uid: 127675 topic_id: 31049 reply_id: 124174[/import]

You don’t show where you defined your display group or called your leafSpawn function. Also, you may find it easier with lots of images to use string concatenation (those ‘…’ double dots) in a loop. Something like this should work.

[lua]local leafGroup = display.newGroup()
local images = {}
for i=1, 4 do
images[i] = “images/leaf”…i…".png"
end

local function leafSpawn()
math.randomseed( os.time())
local index = math.random(1, #images)
local leaf = display.newImage(images[index])
leaf.x, leaf.y = 100, 100
leafGroup:insert(leaf)
end

leafSpawn() --Loop this call if needed[/lua]

This code works to get your images in the local group though you may have to rename your first one from ‘leafs.png’ to ‘leaf1.png’. [import]uid: 168249 topic_id: 31049 reply_id: 124177[/import]

@cooperlabs,

hey, thanks for that.
I forgot to add the spawn function and display group. Yes this seems a much way of doing this.
And I think it works, hard to say as yet because I having real trouble cancelling timers and runtimes.
Have a look and comment if you like hint hint.

Thanks for replying it really does help us new developers to move on and understand better practices. [import]uid: 127675 topic_id: 31049 reply_id: 124336[/import]