AS3:如何将字符串转换为 DisplayObject

AS3: how to convert a string to a DisplayObject

这让我发疯。

Main.as 中,我有一个 按钮 ,当按下它时,会触发一些东西。 在 arrayTambores 中,我定义了一个包含一些 Strings 的数组。

我想要实现的是创建一个大的 movieclip 容器,里面有几个 mc,它们的名字是从那个数组中挑选出来的。

谢谢大家!

这就是我在 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()
        {
        }
    }
}

这是我的 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);
        }
    }
  }
}

此答案假设您有两个启用了 'export for actionscript' 的库对象和 class 名称 heladeraIconcomodinIcon

要在您的代码中实例化它们,您不能使用字符串,因为 AS3 不通过字符串引用 classes(尽管您可以使用 [= 从字符串中获取 Class 引用15=] 但这里没有必要)。

你实际上只是通过你作为 class 输入的任何值来引用它们,所以在你的情况下 heladeraIcon(没有引号,因为它不是字符串,它是 class) .当您选中 Export for actionscript 时,该 class 名称将在您的程序中可用,就像内置的 classes.

因此您的代码应如下所示:

        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);

这里是一个如何使用 AS3 Class 链接正确设置库对象的示例:

  1. 在 Flash/AnimateCC 中,右键单击(或 ctrl+单击 Mac)您的符号并选择 properties.

  2. 在属性 window 中,选中 ActionScript Linkage 部分中的 Export for ActionScriptExport in frame 1 复选框。

  3. 同样在 ActionScript Linkage 部分,输入一个唯一的 class 名称(在这个例子中我输入了 MyCustomClass.

  4. 关闭属性 window。

在库中,您现在应该可以看到链接列下给出的 Class 名称。请记住,对象名称和 Class/Linkage 名称可以不同。

现在有了上面的链接设置,我可以做以下事情:

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