How would I make an array of Movieclips?

8 posts

Flag Post

Hey guys I’m working on my first game in AS3 and I’ve come to a snag. Essentially I can spawn a single enemy, but can’t figure out how to spawn a second one. I took a AS3 class long ago, and I remmeber there was a way where I could just plug in a number into some square brackets.

public var impz:MovieClip = new impja;

stage.addChildAt(impz,1);
impz.x = randomRange(150,450);
impz.y = randomRange(150,450);

Thanks for anyone who helps!

 
Flag Post

spawn them using a loop:

var a:Array = [];
var i:int = 10;
while(i-->0)
{
a.push(new impja);
a[i].x = i*10;
a[i].y = i*10;
stage.addChildAt(impz,1);
}

directly copying this in in place of what you posted should work.

 
Flag Post

Adding them to the document class should be sufficient.

 
Flag Post

o: hmm I’ve replaced it, but it’s not working probably because of where I have it in the code.

public function impcall():void
{
stage.addEventListener(Event.ENTER_FRAME, impgo);
var a:Array = 1;
var i:int = 10;
while (i—>0)
{
a.push(new impja);
a[i].x = i * 10;
a[i].y = i * 10;
stage.addChildAt(impz,1);
}
}

The error I get is access of undefined property of impz. x2

Also a method called impgo which moves the enemy isn’t working either do I have to ajust that for the array? Example of a few lines of impgo

var impx:int = -9;
var impy:int = -6;
if (impz.x <= impz.width / 2 + 50)
{
impz.x = impz.width / 2 + 50;
impx *= -1;
}

Again thank you.

 
Flag Post

You do not create new objects.
Your array is local, hence it cannot be accessed in your method that’s supposed to move the elements of it.
You have to iterate over the array, if you want to manipulate each element.

And:

Originally posted by NineFiveThree:

Adding them to the document class should be sufficient.

 
Flag Post
Originally posted by sandro1:

o: hmm I’ve replaced it, but it’s not working probably because of where I have it in the code.

public function impcall():void
{
stage.addEventListener(Event.ENTER_FRAME, impgo);
var a:Array = 1;
var i:int = 10;
while (i—>0)
{
a.push(new impja);
a[i].x = i * 10;
a[i].y = i * 10;
stage.addChildAt(impz,1);
}
}

The error I get is access of undefined property of impz. x2

Also a method called impgo which moves the enemy isn’t working either do I have to ajust that for the array? Example of a few lines of impgo

var impx:int = -9;
var impy:int = -6;
if (impz.x <= impz.width / 2 + 50)
{
impz.x = impz.width / 2 + 50;
impx *= -1;
}

Again thank you.

Here’s what you want to do:

// 1. Start a new list. I recommend a Vector instead of an array:
private var list:Vector.<MovieClip> = new Vector.<MovieClip>();
// 2. Place all your new movieclips in there. Do this by using a while/for loop.
for (var i:int = 0; i < 10; i++) // start i at zero, and increment it by one each time this code is run. If it is more >= 10, stop.
{
    //2.1 Create new mc
    var newMC:MovieClip = new mcClass();
    //2.2 add that to the array. The push() function puts it at the end.
    list.push(newMC);
    //2.3 Do whatever you want with it. (this can be done before 2.2, if you prefer.
    newMC.x = 3.1415;
    newMC.y = 1.414;
    //2.4 add to the stage/class wherever. again, this can be done whenever after 2.1.
    addChild(newMC);
 
Flag Post

I had made a type, there is no impz, change that to impja.

 
Flag Post
Originally posted by qwerber:

I had made a type, there is no impz, change that to impja.

it’s in the op