Quickly Hiding assets inside a table

I’m just optimizing my ways to keep my lua code short and sweet.

I’ve declared several images, and I actually have to hide many of them, right when the app starts.
So, I’ve wanted to throw them into a lua table and put them all as isVisible=false;

Here is what I’ve done so far:

local stats1 = display.newImage("stats1.png");   
stats1.isVisible=false;  
local stats2 = display.newImage("stats2.png");   
stats2.isVisible=false;  
local stats3 = display.newImage("stats3.png");   
stats3.isVisible=false;  
local stats4 = display.newImage("stats4.png");   
stats4.isVisible=false;  
local stats5 = display.newImage("stats5.png");   
stats5.isVisible=false;  
local stats6 = display.newImage("stats6.png");   
stats6.isVisible=false;  
local stats7 = display.newImage("stats7.png");   
stats7.isVisible=false;  

Eventually the stats images are actually going to be loaded from a json file.

So, I’d like to know if I do this:

statsImages = {"stats1","stats2","stats3","stats4", etc........}  
  
--auto make the elements in statsImages table invisible  
statsImages.isVisible = false;  

The code above obviously doesn’t work. I’m certain i’ll need to loop through the table and perform something on each iteration. However, it doesn’t hurt to try asking if there’s an easier way, besides iterating.

If I were to do iterating, this is how I think I’d do it:

for i,v in ipairs(statsImages) do v.isVisible=false; end  

And as luck would have it, that doesn’t work either.
So, any help would be appreaciated :slight_smile: [import]uid: 154122 topic_id: 27324 reply_id: 327324[/import]

Well there are two easy ways to do this. The easiest way would be to put them all into a group and hide/show the whole group. like this:

[lua]local imageGroup = display.newGroup ()
local stats1 = display.newImage(imageGroup, “stats1.png”);
local stats2 = display.newImage(imageGroup, “stats2.png”);
local stats3 = display.newImage(imageGroup, “stats3.png”);
local stats4 = display.newImage(imageGroup, “stats4.png”);
local stats5 = display.newImage(imageGroup, “stats5.png”);
local stats6 = display.newImage(imageGroup, “stats6.png”);
local stats7 = display.newImage(imageGroup, “stats7.png”);

–Now toggle the isVisible for the group
imageGroup.isVisible = false

–or
imageGroup.isVisible = true

–This will hide everything in the group or show everything in the group.[/lua]

The other way of doing this is to insert everything into a table then just do a loop and hide each item in the table.

Lets say u are using a lua table for this example:

[lua]local imageTable = {}

imageTable[1] = {}
imageTable[1] = display.newImage(“stats1.png”)
imageTable[1].x = 30; imageTable[1].y = 30;

imageTable[2] = {}
imageTable[2] = display.newImage(“stats2.png”)
imageTable[2].x = 60; imageTable[2].y = 60;

imageTable[3] = {}
imageTable[3] = display.newImage(“stats3.png”)
imageTable[3].x = 90; imageTable[3].y = 90;

–Now you have your table set then just hide everything in the table with a loop.

for i = 1, #imageTable do
imageTable[i].isVisible = false
end

–That will run through the table with #imageTable being the max number without nils in the table so in this case #imageTable = 3[/lua] [import]uid: 126161 topic_id: 27324 reply_id: 111021[/import]

I considered putting the images all inside a group, and make the group hidden. I think the only drawback might be wanting to display a single image, or 2 images while the rest stay hidden.

Ok, so I tried your looping idea, and it seems to work:

--make the png image names below, and make invisible too  
local imageTable = {"stats1","stats2","stats3","stats4"}  
for key,value in ipairs(imageTable) do  
 print(key.." "..value);  
 local value = display.newImage(value..".png");   
 value.isVisible = false;  
end  

Yep, it works!
I know it works because I can change isVisible to true, and poof, there they are.

So here’s what doesn’t seem to work.
If I want to make one of the images visible, (after the looping) I get an error returned in the terminal.

So here is what I would try, right below that code above:

--just make one image visible after the looping above  
stats1.isVisible = true;   

The error returned says something like this:
"…attempt to index global ‘stats1’ (a nil value)…"

Does this make sense? [import]uid: 154122 topic_id: 27324 reply_id: 111022[/import]

The problem is your table is just a series of names, when you create stat1 you are creating it locally with in the function with no outside reference. Therefore when you try to access stat1 you cant because you are outside of the loop.
This is how you would need to solve it.

[lua]–make the png image names below, and make invisible too
–This group is only names.
local imageNames = {“stats1”,“stats2”,“stats3”,“stats4”}

–You need to put the actual display objects into a table like this so you can access them once the loop is finished.
local displayImages = {}

for key, value in ipairs(imageTable) do
print(key…" “…value);
displayImages[value] = {}
displayImages[value] = display.newImage(value…”.png");
displayImages[value].isVisible = false;
end

–now you should be able to call the image to turn visible
displayImages[“stats1”].isVisible = true[/lua]

I did not test this but it should work, let me know if you have any problems with it or if i need to explain the problem a different way. [import]uid: 126161 topic_id: 27324 reply_id: 111027[/import]