Array, movieclip, addchild  AS3

Posted on 2009-12-17
Last Modified: 2013-11-11
I have an array that gets populated based on choices a user makes in a form.  Trying to write a function that will then be able to take those array items, convert them to movieclips and then add those mc's to the stage via addChild.  So the array might look like "item1", "item3", "item6" and I just need to know how to get these converted to mc's.
Question by:moglie
    LVL 4

    Expert Comment

    I t depends on what kind of movieclips you want. Empty movieclips? Any kind of item?

    If it's an specific movieclip you can set a class name for it in the library via the linkage property

    So if you already have the array, you can just do that:
    //Supposing that you set a MyCustomMC ClassName to your movieclip in the library
    var customMc:MyCustomMC;
    function convertArrayToMC():void{
      var len = myArray.length;
      for(var i:int = 0; i<len; i++){
        customMc = new MyCustomMC;
        customMc.x = 100; //that's optional
        customMc.y = 100; //that's optional
        this.addChild(customMc); //"this" referrs to mainStage;

    Open in new window


    Author Comment

    There will be specific movieclips in the library with class names such as MyCustomMC1, MyCustomMC2, MyCustomMC3 etc.

    So on the first screen I've got a series of checkboxes and if a checkbox is selected, it pushes a string into the array.  If cb1 is selected it'd push, for example, MyCustomMC1, into the array.  If cb3 was selected it'd push MyCustomMC3 into the array and so on.  When completed, the array might look like ("MyCustomMC1", "MyCustomMC3", "MyCustomMC5").

    When user goes to the next screen, I need to get these "mc's" from the array and add them to the stage (the mc's are different with different content).
    LVL 4

    Accepted Solution

    Well just ad some conditionals ; )
    var customMc1:MyCustomMC1;
    var customMc2:MyCustomMC2;
    var customMc3:MyCustomMC3;
    function convertArrayToMC():void{
      var len = myArray.length;
      for(var i:int = 0; i<len; i++){
          customMc1 = new MyCustomMC1;
          customMc1.x = 100; //that's optional
          customMc1.y = 100; //that's optional
          this.addChild(customMc1); //"this" referrs to mainStage;
        }else if(myArray[i]=="MyCustomMC2"){
          customMc2 = new MyCustomMC2;
          customMc2.x = 100; //that's optional
          customMc2.y = 100; //that's optional
          this.addChild(customMc2); //"this" referrs to mainStage;
        }else if(myArray[i]=="MyCustomMC3"){
          customMc3 = new MyCustomMC3;
          customMc3.x = 100; //that's optional
          customMc3.y = 100; //that's optional
          this.addChild(customMc3); //"this" referrs to mainStage;

    Open in new window


    Author Closing Comment

    I think that'll work.  Luckily I won't have that many movieclips to work with so the conditionals won't be so bad to write.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
    I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
    The goal of the tutorial is to teach the user how to how to record live broadcast.
    The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now