我目前正在尝试通过一个数组和一个循环来制作一个动态菜单。因此,当有人单击数组的第一项时,"menu_bag_mc“将链接到将加载的另一个电影剪辑的内容"menu_bag_mc_frame”(或此数组唯一的某个名称)。下面是我到目前为止的代码:
//right here, i need to make a variable that I can put in the "addchild" so that
//for every one of the list items clicked, it adds a movieclip child with
//the same name (such as menu_bag_mc from above) with "_frame" appended.
//I tried the next line out, but it doesn't really work.
var framevar:MovieClip = menuList[i] += "_frame";
function createContent(event:MouseEvent):void {
if(MovieClip(root).currentFrame == 850) {
while(MovieClip(root).numChildren > 1)
{
MovieClip(root).removeChild(MovieClip(root).getChildAt(MovieClip(root).numChildren - 1));
}
//Here is where the variable would go, to add a child directly related
//to whichever array item was clicked (here, "framevar")
MovieClip(root).addChild (framevar);
MovieClip(root).addChild (closeBtn);
}
else {
MovieClip(root).addChild (framevar);
MovieClip(root).addChild (closeBtn);
MovieClip(root).gotoAndPlay(806);
}
} 有没有办法从数组中创建一个唯一的变量(不管它是什么),这样我就可以用它来命名一个电影剪辑,这样它就可以加载新的电影剪辑了?谢谢
发布于 2010-05-26 04:35:38
你的"menuList“数组是由什么组成的?字符串?对MovieClips的引用?还是别的什么?我将假设它是一个字符串数组。
请记住,addChild方法接受类的实例,而不是类的名称。
我不确定我是否理解您正在尝试做什么,但我假设您正在尝试创建一个您并不真正知道其名称的类的实例(您需要根据所单击的按钮来生成名称)。我可能会这样做:
var menuList:Array = ["foo1", "foo2", "foo3"];
var className:String = menuList[i] + "_frame";
var frameVarClass:Class = flash.utils.getDefinitionByName(className) as Class;
var framevar:MovieClip = new frameVarClass() as MovieClip;
MovieClip(root).addChild(framevar);它所做的是生成所需的类名,并将其存储在className变量中。然后将名称提供给getDefinitionByName,它将返回一个类。然后,我们创建该类的一个实例(framevar),并将其类型转换为MovieClip。然后我们将这个新的MovieClip添加到根目录。
https://stackoverflow.com/questions/2906847
复制相似问题