-
[F8] Arrays, for loops, and _alpha
What's up...
I am trying to work with an array, a for loop and the _alpha property of a movieClip and I'm having some problems with it.
The situation:
On the stage I have 3 movieClips with instance names of "mc1, mc2, and mc3".
They are all on frame one (no other frames in the movie) I'm trying to make them all have an _alpha property of 10% when the movie loads and I want to do it with action Script. So here's what I put in the actions layer...
Code:
var allMC:Array = new Array("mc1","mc2","mc3");
for(var i:Number = 0; i<=3; i++) {
allMC[i]._alpha = 10;
}
This does not work, I would be very greatful if someone could tell what I'm doing wrong here.
Thanks in advance...
-
Senior Member
Originally Posted by aircan7
What's up...
I am trying to work with an array, a for loop and the _alpha property of a movieClip and I'm having some problems with it.
The situation:
On the stage I have 3 movieClips with instance names of "mc1, mc2, and mc3".
They are all on frame one (no other frames in the movie) I'm trying to make them all have an _alpha property of 10% when the movie loads and I want to do it with action Script. So here's what I put in the actions layer...
Code:
var allMC:Array = new Array("mc1","mc2","mc3");
for(var i:Number = 0; i<=3; i++) {
allMC[i]._alpha = 10;
}
This does not work, I would be very greatful if someone could tell what I'm doing wrong here.
Thanks in advance...
The problem is, your array is populated with strings, not references to the movie clips. Probably the easiest way to get this to work is to use bracket reference, like so:
Code:
var allMC:Array = new Array("mc1","mc2","mc3");
for(var i:Number = 0; i<=3; i++) {
this[allMC[i]]._alpha = 10;
}
Hope this helps.
d.
-
Thanks
Yes, that helped completely. I've been trying to figure that out for 3 hours.
Thanks a million!
-
Senior Member
Originally Posted by aircan7
Yes, that helped completely. I've been trying to figure that out for 3 hours.
Thanks a million!
Yeah, it took me a while to learn the nuances of array notation like this. Some people will also use Eval, but this is the preferred method. Note, you could also push direct references to the movie clips and access them like you had in your first example. Like so:
Code:
var allMC:Array = new Array();
//note, no quotes around the movie clip names;
allMC.push(this.mc1);
allMC.push(this.mc2);
allMC.push(this.mc3);
for(var i:Number = 0; i<=3; i++) {
//see ma! no this or brackets;
allMC[i]._alpha = 10;
}
Now the array is holding direct references to the movie clips, not strings, so it works in this case. When i'm creating a bunch of movie clips at one time (like in a menu or list for example) i usually push them into an array for easy handling later.
d.
-
Ok cool, thanks for the futher info. I couldn't find any info or tutorials on what I was trying to do specificly trying to do (array of mc's) so I was getting pretty frustrated there for a while.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|