adding Library elements dynamically to the stage
One of the biggest changes to Actiosncript 3.0 for me (which took me forever to figure out) is the method to add an object from the library by its identifier, in case that identifier was ordered.
take this screenshot for example:
to add an instance of the switchButtons movieClip to the stage all you have to write is:
var sb = new switchButtons(); stage.addChild(sb);
now if switchButtons does not have any class associated with it, flash will create a class for it that extends MovieClip. we will not be able to see it as a physical file in our folder but it will be created on compile time.
now.. in the other case where there are 4 movieClips called ch1, ch2, ch3, ch4 :
the way to add them is to say var ch1 = new ch1(); etc…
but what if we have 300 of them?
in AS2.0 we could write:
for (var i:umber=0; i<300; i++){
_root.attachMovie("ch"+i , "newCh"+i, _root.getNextHighestDepth() );
}
well, Actionscript does not work this way, and you can’t say: var “ch”+i = new “ch”+i();
so here’s the proper syntax:
import flash.utils.getDefinitionByName; ...
..
for (var i:uint=0; i<300; i++){
var ClassReference:Class = getDefinitionByName("ch"+i) as Class;
var _ch:Sprite = new ClassReference();
_ch.name = "ch"+i;
stage.addChild(_ch);
_ch=null;
}
..
and then at anytime when we need to access any of those guys all we have to write is:
var _ch = stage.getChildByName("ch"+order);
where order is the number of the movieClip we need to do stuff with