Search code examples
actionscript-3

AS3: how to convert a string to a DisplayObject


This is driving me crazy.

In Main.as I have a button that, when pressed, trigger some stuff. In arrayTambores I define an array with some Strings in it.

What I want to achieve is to create a big movieclip container with several mc inside, whose names are picked from that array.

Thanks you guys!

So this is what I have in ArrayTambores:

package 
{
    public class arrayTambores
    {
        public static var tempArray: Array = new Array();
        public static var bigArrayTambor1: Array = new Array();
        public static var randomPos:Number = 0;
        public static var a:int = 0;
        public static var z:int = 0;


        tempArray[0] = "heladeraIcon";
        tempArray[1] = "comodinIcon";

        for (a = 0; a < 2; a++)
        {
            tempArray.unshift("microondasIcon");
        }

        for (a = 0; a < 5; a++)
        {
            tempArray.unshift("duchaIcon");
        }


        bigArrayTambor1.length = tempArray.length;

        for (var z: int = 0; z < bigArrayTambor1.length; z++)
        {
            randomPos = int(Math.random() * tempArray.length);
            bigArrayTambor1[z] = tempArray.splice(randomPos,1)[0];
        }

        public function arrayTambores()
        {
        }
    }
}

And this is my Main.as:

package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class Main extends MovieClip
    {

        public function Main():void
        {
            var Tambor1_mc:MovieClip = new MovieClip();//This is the container mc

        /*I already convert some bitmaps to movieclips in my Library, and assign each of one a class name. I.e., for the var comodinIcon, the class to the comodin.jpg in the Library is comodin. And of course I set each one to Export for ActionScript and Export in frame 1.*/

        var comodinIcon:comodin = new comodin();
        var duchaIcon:ducha = new ducha();
        var heladeraIcon:heladera = new heladera();
        var microondasIcon:microondas = new microondas();


        // BUTTON
        makeButton(my_mc, my_mc_click);

        function my_mc_click(evt: MouseEvent):void
        {

            Tambor1_mc.x = 132;
            Tambor1_mc.y = 250;


            var clipA1:String = new String();
            clipA1 = arrayTambores.bigArrayTambor1[0];
            // HERE is where it fails, because it can´t convert clipA1, which is a String, //to a flash.display.DisplayObject

            Tambor1_mc.addChild(DisplayObject(clipA1));

            stage.addChild(Tambor1_mc);
        }
        function makeButton(which_mc: MovieClip, clickFunction: Function):void
        {
            which_mc.buttonMode = true;
            which_mc.useHandCursor = true;
            which_mc.mouseChildren = false;
            which_mc.addEventListener(MouseEvent.CLICK, clickFunction);
        }
    }
  }
}

Solution

  • This answer makes the assumption that you have two library objects with 'export for actionscript' enabled and class names of heladeraIcon & comodinIcon.

    To instantiate those in your code, you can't use strings because AS3 doesn't reference classes via strings (Though you can obtain a Class reference from a string using flash.utils.getDefinitionByName() but that isn't necessary here).

    You actually just reference them by whatever value you put in as the class, so in your case heladeraIcon (no quotes because it's not a string, it's a class). When you check Export for actionscript, that class name becomes available in your program just like built-in classes.

    So your code should look something like this:

            tempArray[0] = heladeraIcon; //match whatever you put in the library object's properties as the class name
            tempArray[1] = comodinIcon;
    

            //you have get a reference to the class
            var clipClass:Class = arrayTambores.bigArrayTambor1[0] as Class;
    
            //then instantiate that class
            var clipA1:DisplayObject = (new clipClass()) as DisplayObject;
    
            Tambor1_mc.addChild(clipA1);
            stage.addChild(Tambor1_mc);
    

    Here is an example how to properly setup your library object(s) with AS3 Class linkage:

    1. In Flash/AnimateCC, right-click (or ctrl+click on Mac) your symbol and choose properties.

    2. In the properties window, check the Export for ActionScript & Export in frame 1 checkboxes in the ActionScript Linkage section.

    3. Also in the ActionScript Linkage section, enter in a unique class name (in this example I've put in MyCustomClass.

    4. Close the properties window.

    In the library, you should now see the Class name that was given under the Linkage column. Keep in mind, the object name and Class/Linkage name can be different.

    Now with the above linkage setup, I can do the following:

    var clipClass:Class = MyCustomClass;
    var clipA1:DisplayObject = (new clipClass()) as DisplayObject;
    addChild(clipA1);
    

    screenshot showing linkage settings