从数组actionscript 3创建变量

时间:2010-05-25 17:19:12

标签: flash actionscript-3 variables arrays

我正在尝试通过数组和循环创建动态菜单。因此,当有人点击数组的第一项时,“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);
}
} 

有没有办法从数组中创建一个唯一的变量(无论它是什么),以便我可以在它之后命名一个movieclip,以便加载新的movieclip?感谢

1 个答案:

答案 0 :(得分:0)

你的“menuList”数组是由什么组成的?字符串?参考MovieClips?或者是其他东西?我会假设它是一个字符串数组。

请记住,addChild方法接受Class的实例,而不是Class的名称。

我不确定我理解你要做什么,但我假设你正在尝试创建一个你不知道名字的类的实例(你需要根据什么按钮生成名称)点击了)。我可能会做这样的事情:

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命名,返回一个Class。然后,我们创建该类的实例(framevar)并将其强制转换为MovieClip。然后我们将这个新的MovieClip添加到root。