这让我发疯了。
在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);
}
}
}
}
答案 0 :(得分:2)
这个答案假设你有两个图书馆对象,其中包括&quot; export for actionscript&#39;已启用且类名为heladeraIcon
&amp; comodinIcon
。
要在代码中实例化那些,你不能使用字符串,因为AS3不通过字符串引用类(尽管你可以使用flash.utils.getDefinitionByName()
从字符串中获取类引用,但这不是&#这是必要的)。
你实际上只是通过你作为类放入的任何值来引用它们,所以在你的情况下heladeraIcon
(没有引号,因为它不是一个字符串,它是一个类)。当您检查Export for actionscript时,该类名在您的程序中可用,就像内置类一样。
所以你的代码应该是这样的:
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类链接正确设置库对象的示例:
在Flash / AnimateCC中,右键单击(或按住c键+单击Mac)您的符号,然后选择properties
。
在属性窗口中,查看Export for ActionScript
&amp; ActionScript Linkage 部分中的Export in frame 1
个复选框。
同样在 ActionScript Linkage 部分中,输入一个唯一的类名称(在此示例中,我已放入MyCustomClass
。
关闭属性窗口。
在库中,您现在应该看到在Linkage列下给出的类名。请记住,对象名称和类/链接名称可以不同。
现在通过上述链接设置,我可以执行以下操作:
var clipClass:Class = MyCustomClass;
var clipA1:DisplayObject = (new clipClass()) as DisplayObject;
addChild(clipA1);