AS3 array of movieclips

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);
		}

I hope my question/issue makes sense…

Thank you.

1 Like
var myArray:Array = new Array; 

var one:MovieClip = instance1; 

var two:MovieClip = instance2; 

myArray.push(one); 

myArray.push(two); 

i don’t think you need the for loop unless you have a bunch of movieclips you want to add.

Are the movieclips your referencing already on the stage or are you gonna place them with code (addChild)?

If they’re already on the stance and have an instance name you can skip the part of giving them a var and just push them into the array.

myArray.push(instance1)

hope that helps.

this should work

var myArray:Array = new Array();
var one:MovieClip = instance1;
var two:MovieClip = instance2;

myArray.push(one);
myArray.push(two);

for(var i:uint = 0; i < myArray.length; i++)
{
myArray[i].x += 100; //example of accessing the array elements
}

or maybe

var myArray:Array = new Array();

myArray.push(instance1);
myArray.push(instance2);

for(var i:uint = 0; i < myArray.length; i++)
{
myArray[i].x += 100; //example of accessing the array elements
}

or like this

var myArray:Array = new Array(instance1, instance2);

You don’t need loop here, just myArray.push(one), myArray.push(two);
Or myArray:Array=[one,two]

Edit: doru was faster :slight_smile:

Edit2: and marcofolio :)))

1 Like

marcfolio/golle -

That helps. Thank you for clarifying this for me.

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.

Thanks again. You guys are great.

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 :slight_smile:

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

:slight_smile:

Thank you.

no pushing = happy me.

Arrays and good stuff. It took me a long time to finally harness them. These are some valuable code snips.

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.

var imgAL:Number = imgArray.length;

for(var i=0; i<imgAL; i++) 
	{
		addChild (imgArray[]);
	}

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.

Is there a reason why you are not loading there images externaly but pulling them for the library?

This creates a Sprite for each image in your array, adds it to the display list, and then adds the corresponding image to the created Sprite.

var sprite:Sprite;
for(var i:int = 0; i < imgArray.length; i++) 
{
    sprite = new Sprite()
    addChild(sprite);
    
    sprite.addChild(imgArray[i]);
}

When you want to access not the image but the Sprite (its container) you can simply:

Sprite(yourImage.parent).somePropery = "someValue"

or

(yourImage.parent as Sprite).somePropery = “someValue”

:smiley:

var imgArray:Array = [];
for (var i=1; i<=20; i++) {
	imgArray.push(addChild(new(Class(getDefinitionByName("Image"+i)))(0,0)));
}

Tean,

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!

Komapeb / FlashTang,

:slight_smile: Sincerely, Thank you!

imgArray.push(addChild(new(Class(getDefinitionByName("Image"+i)))(0,0)));

LOL! :smiley:

komapeb,

when I try to run your example I get the following error message:

“TypeError: Error #1034: Type Coercion failed: cannot convert img1@10bc4ac1 to flash.display.DisplayObject.
at portfolio_fla::mc_container_1/frame1()”

  • EDIT: I’m an idiot. all is well. silly mistake. Thank you, again.