Creating Display Objects Inside of a Loop

How would I go about referencing newImage display objects created inside of a loop?
Can I assign them dynamic variable names somehow?

I’d like to be able to put them into a display group after the loop finishes executing so I can manipulate them together.

I appreciate any help and or code samples. [import]uid: 4621 topic_id: 1716 reply_id: 301716[/import]

Include them inside the loop into the group you want them to be. [import]uid: 5712 topic_id: 1716 reply_id: 5027[/import]

Thank you, Mike.

May I see an example please?

How would I reference them outside of the loop when it completes?

The dynamic variable names are what are primarily throwing me off, as I’m not sure how to implement those in Lua yet. [import]uid: 4621 topic_id: 1716 reply_id: 5039[/import]

Dynamic variable names seems like an easily answered topic from someone that knows Lua.

Anyone out there? :slight_smile:

My normal Google routine hasn’t turned up anything useful yet… going to try again now. [import]uid: 4621 topic_id: 1716 reply_id: 5051[/import]

I figured it out using the global namespace.

For example:[lua]myVarPrepend = “month”

function countMonths()
for i = 1, 12 do
_G[myVarPrepend…“foo”…i] = i
end
end

countMonths()
print( monthfoo8 )[/lua] [import]uid: 4621 topic_id: 1716 reply_id: 5052[/import]

The global namespace will be a slow way of accessing the display objects, include them in a group

Declare the group outside the loop

local imageGroup  
  
for i = 1, 12 do  
 local newImage = display.newImage( "imageName.png" )  
 imageGroup:insert( newImage )  
 imageGroup.myImageName\_..i = newImage  
end  

Sorry I cant test this at the moment but I think it should work, you can access individual images like imageGroup.myImageName_5, you can also pass an array with a image name & file name and have it loop through the array taking the “image name” value for the imageGroup.myImageName and the file name to be passed to display.newImage() [import]uid: 5354 topic_id: 1716 reply_id: 5093[/import]

Many thanks, Matt! Very much appreciated.
I follow your logic and I think it’s a much better solution. [import]uid: 4621 topic_id: 1716 reply_id: 5136[/import]

Hi Krunal

Check http://www.lua.org/manual/5.1/manual.html#2.5.4

basically use … between the variable and the i

ie

filename = “checkbook”
filename = filename … “.tmp” [import]uid: 5354 topic_id: 1716 reply_id: 21140[/import]

hello Matt,

I would like to thnks for your post. but i hv one query in your code,

How to append my loop variable with declared variable,
Like
local Myvar1 = “Hello”
local Myvar2 = “Testing for my first”
local Myvar3 = “Test This is my frist”

for i = 1, 3 do
– i append with my declared variable like Myvar+""+i
Myvari:setTextColor(0,0,0)
end
I hv try with like Myvar…""…i but not working…

Thankx
Krunal
[import]uid: 33757 topic_id: 1716 reply_id: 21137[/import]

Hi Matt,

Thanks for reply.

I’d also do this Myval…i but its not working. :frowning:

for i=1, 9 do
Myval…i:setTextColor(0,0,0)
end

is there any ohter way to do this…???

Thankx
Krunal
[import]uid: 33757 topic_id: 1716 reply_id: 21142[/import]

You need to put the variables in a table to do that

local t = {}

t[“Myvar1”] = “Hello”
t[“Myvar2”] = “Testing for my first”
t[“Myvar3”] = “Test This is my frist”

for i = 1, 3 do

print( t[“Myvar”…i] )

end [import]uid: 5354 topic_id: 1716 reply_id: 21145[/import]

Hi matt,

Thanks for this another way,

but i want to use this i variable append with my declared variable, b’cas in my code i wanna use this Myvar1 variable,and work with array its complex.

is it ok with that, if i am use addEventListener on this arrya variable…?

if you know that how to do this i with my variable then plz do this.

we hv doing this things using c# like Myval + “” + i then we get the variable name but in LUA i m not figure out.

Thankx
Krunal [import]uid: 33757 topic_id: 1716 reply_id: 21150[/import]

Does anyone know why i get the error:

“Desktop/MixtureFolder/play.lua:138: attempt to index field ‘naviButton’ (a nil value)”

from the following code? Line 138 is the one trying to name the display object.

[lua]local overlayLayer = display.newGroup()

local navButton = {}

for i = 1, 7 do
if (i == 1) then
navButton = display.newText( “Close” , 0, 0, buttonFont, 40 )
navButton.y = (screenH2 - 220)
hideButtons()
elseif (i > 1 and i <= 4) then
navButton = display.newText( “More” , 0, 0, buttonFont, 40 )
navButton.y = (screenH2 - 220) + ((i - 1) * 400)

elseif (i > 4 and i <= 7) then
navButton = display.newText( “Less” , 0, 0, buttonFont, 40 )
navButton.y = (screenH2 - 220) + ((i - 4) * 400)

end
navButton.x = screenW2
navButton:setTextColor(textR, textG, textB)
overlayLayer:insert(navButton)
overlayLayer.naviButton…i = navButton
end[/lua] [import]uid: 13522 topic_id: 1716 reply_id: 27073[/import]

@krunalbarot: is it ok with that, if i am use addEventListener on this arrya variable…?

Yes you can do that. I’m sorry you think tables (remember, in Lua there aren’t arrays, only tables) are complicated but Matt is correct, what you want is done with tables.

@cubbox: Matt already explained, you can’t append onto variable names like that. You can append onto strings, but variable names aren’t strings. As he pointed out, what you want is done with a table. [import]uid: 12108 topic_id: 1716 reply_id: 27229[/import]

[lua]local myvar = {}

myvar[1] = “Hello”
myvar[2] = “Testing for my first”
myvar[3] = “Test This is my frist”

for i = 1, 3 do

print( myvar[i] )

end[/lua]

vs dynamic variables

[lua]myVarPrepend = “month”

function countMonths()
for i = 1, 12 do
local str = myVarPrepend…“foo”…i…"="…i
local fn = loadstring(str)
assert(fn)()
end
end

countMonths()
print( monthfoo8 ) – 8[/lua]

but why bother?! :wink: [import]uid: 6645 topic_id: 1716 reply_id: 27282[/import]