Since this is my first post in the forum here I think it only appropriate to thank each of you for your great work and offerings. I have learned quite a bit by purchasing and tweaking/playing with the code… a sort of “let’s see what happens when I do ‘X’”.
I have one instance of two different MovieClips on my stage and would like to create an array for them so that I can reference the array as opposed to the instances later in my code. This would make my life, obviously, a lot easier.
I am missing something here, as you will see below, in my attempt to resolve this.
it is important to note that my instances are instantiated higher up in my code.
var myArray:Array = new Array;
var one:MovieClip = instance1;
var two:MovieClip = instance2;
for (var n:Number = 0; n<1; n++)
{
myArray.push(one);
myArray.push(two);
}
marcfolio, I am asking because I see myself using a similar technique in the future and would like to know how to properly loop through pushing an instance with each pass.
If have just two (or any known number of existing instances) you can initialize the array with them when you declare it:
var mcArray:Array = [instance1, instance2];
That’s all! No pushing required
If the instances are already created it is not practical to assign them to an array in a loop, but what you may want to do is look at assigning the instances to the array in a loop as they are created:
var myArray:Array = new Array();
for (var i:int = 0; i<num; i++){
var mc:MovieClip = new MovieClip();
//do some stuff with the newly created MovieClip
...
...
// then add a reference to it to the array.
myArray.push(mc);
}
You can then refer to the MovieClips as myArray[0], etc
I’ve taken what you’ve said and tried to push on a little farther.
I have created an array of bitmaps.
var imgArray:Array = [];
var img1:Image1 = new Image1 (90, 90);
var myImg1:Bitmap = new Bitmap(img1);
imgArray.push(img1);
var img2:Image2 = new Image2 (90, 90);
var myImg2:Bitmap = new Bitmap(img2);
imgArray.push(img2);
// ... this continues for ~12 bitmaps.
Then I am trying to create a for loop to create a sprite on stage for each bitmap and inject my bitmap[x] within the sprite.
I don’t need to tell you that the above doesn’t work. Should I create a separate associated array of empty sprites (whose array length is equal to the imgArray lengh? This is where I get fuzzy and overheat… deficiency begins here.
So, I guess my question lies in trying to get my bitmap images to display on my stage; and doing so efficiently.
*Am I at least on the right track here? I have been hand-coding all of the variables up until this point and I am trying to make my life easier by learning how to use arrays.
No. I am doing this out of habit. I am trying to make my life easier by learning about arrays and in doing so if I am able to learn more on how I can make work easier for myself then that is a plus!