all groups > flash actionscript > august 2005 >
You're in the

flash actionscript

group:

Accessing an array off another array's event


Accessing an array off another array's event bwm_razel
8/30/2005 8:18:50 PM
flash actionscript:
Alright, as of now, I have a bunch of rollOver/rollOut code in a .as file so
the programmer can just drag a button onto the stage and a popup box, give them
instance names, and it will automatically work. Right now, the code looks like
this, very long:

roll1_btn.onRollOver = function() {
oval_mc._visible = true;
}
roll1_btn.onRollOut = function() {
oval_mc._visible = false;
}
roll2_btn.onRollOver = function() {
oval1_mc._visible = true;
}
roll2_btn.onRollOut = function() {
oval1_mc._visible = false;
}
roll3_btn.onRollOver = function() {
oval2_mc._visible = true;
}
roll3_btn.onRollOut = function() {
oval2_mc._visible = false;
}
roll4_btn.onRollOver = function() {
oval3_mc._visible = true;
}
roll4_btn.onRollOut = function() {
oval3_mc._visible = false;
}
roll5_btn.onRollOver = function() {
oval4_mc._visible = true;
}
roll5_btn.onRollOut = function() {
oval4_mc._visible = false;
}




Now, what I was thinking is this: I create two different arrays:

myArray1 = [roll1_btn, roll2_btn, roll3_btn, roll4_btn, roll5_btn];
myArray2 = [oval_mc, oval1_mc, oval2_mc, oval3_,mc, oval4_mc];



Now what I want to do is say the same thing as above, with that long drawn out
code, but using these arrays to condense it. Is it possible?
Re: Accessing an array off another array's event NSurveyor
8/30/2005 9:01:22 PM
roll_btn_arr = [roll1_btn, roll2_btn, roll3_btn, roll4_btn, roll5_btn];
oval_mc_arr = [oval_mc, oval1_mc, oval2_mc, oval3_,mc, oval4_mc];
for(var i=0;i<roll_btn_arr.length;i++){
roll_btn_arr[ i ].oval = oval_mc_arr[ i ];
roll_btn_arr[ i ].onRollOver = function(){
this.oval._visible = true;
}
roll_btn_arr[ i ].onRollOut = function(){
this.oval._visible = false;
}
}

Re: Accessing an array off another array's event NSurveyor
8/31/2005 12:00:00 AM
Sure, no problem. So first, we have all the roll_btn's in an array, and all the
oval_mc's in an array. We use the for in loop to loop through all the buttons.
roll_btn_arr[ i ] will be a different button each iteration (when i changes).
So, say i is 0. We look at roll_btn_arr[ 0 ] (which is roll1_btn). We then
assign it a reference to its oval counterpart: oval_mc_arr[ 0 ] (which is
oval_mc). So, the roll_btn_arr[ 0 ] now KNOWS which oval it toggles the
visibility of. So basically, that line creates a variable called oval in each
roll_btn which is a reference to that btn's oval counterpart. Then inside the
code, this.oval will be the: roll_btn_arr[ i ].oval. However, you might wonder
why can't I just use oval_mc_arr[ i ]._visible = false; ? Well, eventually i
will become 4 (or whatever iterates lasts). When the onRollOver and onRollOut
are called, it will take the value of i, which is now 4 and then evaluates it,
making all the button only toggle one oval. But because I have created a
reference to the oval for each btn, they can all target their own oval, with
this.oval. Hope that clears things up.


Re: Accessing an array off another array's event NSurveyor
8/31/2005 12:00:00 AM
Re: Accessing an array off another array's event bwm_razel
8/31/2005 12:00:00 AM
Re: Accessing an array off another array's event bwm_razel
8/31/2005 12:21:39 PM
NSurveyor, thanks a lot for the code. You had an extra comma (oval3_,mc,) in
the second array, but once you remove that it works fine. I also modified it a
bit to use the for..in method, simply because that's what I feel most
comfortable with. Right now it looks like this, and is working fine:

roll_btn_arr = [roll1_btn, roll2_btn, roll3_btn, roll4_btn, roll5_btn];
oval_mc_arr = [oval_mc, oval1_mc, oval2_mc, oval3_mc, oval4_mc];

for (var i in oval_mc_arr) {
oval_mc_arr[ i ]._visible = false;
}

for (var i in roll_btn_arr) {
roll_btn_arr[ i ].oval = oval_mc_arr[ i ];
roll_btn_arr[ i ].onRollOver = function(){
this.oval._visible = true;
}
roll_btn_arr[ i ].onRollOut = function() {
this.oval._visible = false;
}
}

stop();

Now my question is this: do you think you could just explain how this works a
bit? If you don't have time then that's fine, but I get how to define the
arrays, and how to set up the for loop, but I don't get what the for loop is
doing.

For example, what exactly is roll_btn_arr[ i ].oval? And how does
this.oval._visible work?

Thanks again for the help.
AddThis Social Bookmark Button