Groups | Blog | Home
all groups > flash actionscript > july 2005 >

flash actionscript : Looping, variables, and path names


David Stiller
7/19/2005 5:00:59 PM
Have A Banana,

[quoted text, click to view]

In the above for() loop, i begins as 0 (zero) and continues until 33.
The assumption, then, is that you have 34 movie clips (zero to 33). Each
clip must have an instance name, and it appears as if your instances are all
contained within another movie clip whose instance name is pictureBox.

For sake of discussion, let's say your landmark clips have instance
names like this: landmark0, landmark1, landmark2, and so on. You're hoping
to set the _visible property of each clip to false. In other words ...

_root.pictureBox.landmark0._visible = false;
_root.pictureBox.landmark1._visible = false;
_root.pictureBox.landmark2._visible = false;
/// etc. through 33

You can accomplish this with the array access operator, which allows you
to use strings in your dot notation.

_root.pictureBox.landmark0

.... is the same as ...

_root["pictureBox"].landmark0

.... for esample, which is also the same as ...

_root.pictureBox["landmark0"]

.... again, notice where the dots are replaced by brackets, and that the
brackets contain strings. As it happens, you can also use variables in your
array access operators, as long as, in the end, the result is a string. In
ActionScript, you can concatenate something like "landmark" (a string) and i
(a number) and get a string.

So your for() loop might look like this:

for(i = 0; i <= 33; i++) {
_root.pictureBox["landmark" + i]._visible = false;
}

Make sense?

Now, to add these items to an array, you'll have to use an Array class
method. Your best bet is to look up the Array class in the ActionScript
Language Reference. Look up Array.push() and you'll get what you're after.
By using the equals sign the way it is currently in your code, you're merely
resetting the whole array object each time to a number, rather than adding
an item to your array.


David
stiller (at) quip (dot) net
Tackling the ActionScript Language Reference
http://www.quip.net/tutorials/

Have A Banana
7/19/2005 8:49:25 PM
First, I will explain my project: I have a map with several landmarks or
buildings on it. I also have pictures of each (named according to legend
position, 0 - 33) in a movie clip and want to put them into an array.

Here is what i'm thinking of doing:

for(i = 0; i <= 33; i++){
// I can do the visible after. it's the array I need.
_root.pictureBox.*i*._visible = false;
_root.pictureArray = _root.pictureBox.*i*;
}

I put *s around the places where I want to use the variable. Is this possible?
Thanks in advance for your replies.
Have A Banana
7/20/2005 12:00:00 AM
I'll try not to waste any more of your time. :D Once again, thanks, as I now
have two more things learned since getting up this morning. I also have a few
less headaches and one finished project. I'll go ahead and stop talking now.
Have A Banana
7/20/2005 12:00:00 AM
Thank you once again; this works exactly how I wanted it to now. This saves me
some trouble, as I don't even have to put the clips into that array. Another
thing I believe in is learning something new every day. Much appreciated!
David Stiller
7/20/2005 11:02:24 AM
Have A Banana,

[quoted text, click to view]

Glad to hear it, Banana.

To clarify, the array access operator can be used in object paths, even
if they aren't arrays. I don't know why it's called the "array access
operator," because that makes it sound like it's only for arrays.

No worries on that bit about the array, brackets, and i. I've taken to
using n instead of i on these forums for exactly the reason you mentioned.
I believe the forum sees bracket-i-bracket as a mechanism to set certain
words in italics, and coding with those characters often disappears. Since
n isn't an HTML element (and i is), n doesn't seem to do the same thing to
the forums.


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

David Stiller
7/20/2005 12:09:30 PM
[quoted text, click to view]

You haven't done it yet. :)

[quoted text, click to view]

Excellent!


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

Have A Banana
7/20/2005 1:03:17 PM
I will start working with your suggested method here in a moment, but I wanted
to reply first. I want to thank you for responding.

You can use array acess operators, or brackets, even if the objects are simply
movie clips contained in movie clips and not already in an array? The whole
goal here was to find a way to access my clips easily, which is why I have the
picture array. If I can simply use the array format any time, then why bother
putting them into an array? (I'm wondering if I just misunderstood you, but
I'll find out soon enough)

Lastly, I don't mean to nitpick, but my for loop didn't just set the array
equal to the number the way I wrote it in my post. Somewhere between my post
and yours, your quote removed my brackets and i.
AddThis Social Bookmark Button