Search code examples
actionscript-3evaladdchild

AS3: Addchild dynamically using varying identifier names


Im currently learning as3 but I come from several years of as2 experience, so what I was used to do is attach movieclips using string with a numeric variable to get the right identifier like this and maybe I was thinking about using eval here but of course its not available in as3 so here is what I was doing in as2:

currentlevel=1;
myString = "levelMc"+currentlevel;

And then of course use the constructed string in "attachMovie" function to get the right clip attached.

attachMovie(myString,myString+mydepth,mydepth);

But I see in as3 all is about classes and all different so I cant seem to find a way to dynamically get the right class name for the child to be added, is there an actual way to make that possible? because as far as I have gone I see that I should get the class name to make new object and then put that in "addChild" so therefore I cant find a way to pass any kind of dynamically constructed string or element refering my needed clip there, is it still possible?. Thanks beforehand for any clues =)

EDIT I added the attachmovie call as a clarifying line, I know all you got what I was asking but as I cant find a right approach I prefer to clarify. Additionally I shouldnt have mentioned the "eval" here, sorry, I just can ask my as2 with string usage

EDIT Solved, thanks to you all guys, you saved me, I wish I could give you all kudos because all of your ideas and tips are awesome and really helpful, I marked that answer as correct because it was the one that lead me to getDefinitionByName which is what Im going to use in the end, but as said all tips and ideas to face new as3 world will help me greatly, so as said I wish I could give reputation to you all, thanks a lot =)


Solution

  • You still can set a name for your object, for instance:

    var mc : MovieClip;
    for (var i : int = 0; i < 5; i++) {
       mc = new MovieClip();
       mc.name = "myMc"+ i;
       addChild(mc);
    }
    

    and you can get a specific child using:

    this["myMc1"] as MovieClip
    

    or

    this["myMc"+ i] as MovieClip;