PDA

View Full Version : AS3 movie clip instantiation...


spirton
09-17-2007, 10:22 PM
hello

after playing with AS1 3 years ago I returned this year to find half of what i knew changed. Still determined to make AS3 work for me I wonder:

How can I dynamicly istantiate a group of movie clips?

the following code doesn't work of course but it gives an idea of what i'm trying to do.

for (var i:int = 0; i < 50; i++) {
var starlet[i]:Star =new Star();
starlet[i].x = Math.floor(Math.random( )*550);
starlet[i].y = Math.floor(Math.random( )*400);
addChild(starlet[i]);
}

any ideas?
thnx a lot!

senocular
09-17-2007, 10:27 PM
you can't use var/typing with [] (or .). As long as starlet is dynamic (i.e. an array), just use

starlet[i] = new Star();

spirton
09-17-2007, 10:35 PM
i tried this code and it works but i can't refference the moviclips after. for example say sky5.visible=false.

var sky:Array = new Array( );
for (var i:int = 0; i < 50; i++) {
sky.push(new Star( ));
sky[i].x = Math.floor(Math.random( )*550);
sky[i].y = Math.floor(Math.random( )*400);
addChild(sky[i]);
}

senocular
09-17-2007, 10:40 PM
sky5 does not equal sky[5]. Using [] separates references: referenceA["referenceBInA"], just like a dot but with a different syntax.

If you want your dynamic variable to be sky5, you need to either declare each of those variables individually with var (outside of the loop), or use

this["sky"+i] = new Star();

and reference them later using this.sky5.visible ... etc. Note that the this is important if you are not declaring them individually with var because you are creating these dynamically rather than having them declared beforehand.

spirton
09-17-2007, 11:02 PM
hey, it works! tons of gratitude comming your way! thanx a million and if you happen to be comming to crete i'll buy the raki!

thnx again.:)