Reputation: 117
I'm currently working on a game prototype in Flash and in the past I've normally avoided arrays, meaning I'm relatively new to them but in the game I'm currently making, there is an automated element thus I now must move into the world of arrays.
So, essentially what I'm trying to do is have 7 objects, let's say boxes for example each has an instance of box + their number (box1, box2, etc.) in my game. 6 of these boxes are invisible except for one of them (box1). But If I click a button, the next box becomes visible (box2, box3, etc.).
So what I did was I created an array containing 7 values, from "1" to "7"
Then I created a an Enter_Frame event that I want to have find which of the boxes are invisible, so here's what I first tried:
var array1:Array = ["1", "2", "3", "4", "5", "6", "7"];
So this is obviously the array, and then I created this:
if(this["box"+ array1].visible == true) {
// Trace The Visible Boxes //
}
So I know this doesn't work but essentially what I'm trying to do is detect which of the boxes are visible and then trace those boxes. Would really appreciate any help or guidance, thanks!
Upvotes: 0
Views: 45
Reputation: 1806
You don't really need an array for that, just check boxes from 1 to 7:
for (var i:int = 1; i < 8; i++)
{
if(this["box" + i].visible)
{
trace("Box " + i + " is visible");
}
}
But if you NEED to do that with an array because there would be some complex values, it would be
for (var i:int = 0; i < array1.length; i++)
{
if(this["box" + array1[i]].visible)
{
trace("Box " + i + " is visible");
}
}
Upvotes: 3