flash - In Actionscript 3, how do I create movieclips through a looping statement? -


the statement for takes triple argument

(initial value of i, condition cease looping, i increment)

i want create different movie clip each time loop goes on.

so, tried:

for (i = 0; < 9; i++){     var unit+i:movieclip = new movieclip() } 

but, triggers following error:

1086: syntax error: expecting semicolon before plus"

what's correct syntax, then?

to address issue. cannot create var dynamically using var keyword. doing var unit+i give syntax error.

you can create array ahead of time , put objects in (as per @panzercrisis's answer acceptable).

other ways can this:

if current context dynamic (like if timeline code or part of dynamic class movieclip), this:

this["unit"+i] = new movieclip(); 

from compile time checking , code maintenance perspective, little sloppy in opinion, quick timeline projects it's acceptable.

you not store clip anywhere display list:

for(var i:int=0;i<9;i++){     var mc:movieclip = new movieclip();     //do whatever need clip     //like draw instance     mc.graphics.beginfill(0xff0000);     mc.graphics.drawrect(0,0,100,100);      addchild(mc); //add display list can see } 

if never need remove clip, way great.


or, add them container display object

var clipcontainer:sprite = new sprite(); //sprite same movie clip without timeline stuff addchild(clipcontainer);  for(var i:int=0;i<9;i++){     var mc:movieclip = new movieclip();     //do whatever need clip     //like draw instance     mc.graphics.beginfill(0xff0000);     mc.graphics.drawrect(0,0,100,100);      clipcontainer.addchild(mc); } 

the clipcontainer act same array, can access it's children. also, moving/scalling container in turn move/scale it's children


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -