从阵列的ActionScript 3创建变量阵列、变量、ActionScript

2023-09-08 15:31:57 作者:ζ已风干的迷茫°

我目前正试图通过阵列和一个循环,使动态菜单。因此,当有人按下数组的第一项menu_bag_mc它将链接到内容menu_bag_mc_frame(或一些名称,这将是唯一的这一阵列)是另一个的movieclip,将加载。下面是code我到目前为止有:

I'm currently trying to make a dynamic menu via an array and a loop. So when someone clicks on the first item of the array, "menu_bag_mc" it will link to the content "menu_bag_mc_frame" (or some name that will be unique to this array) that is another movieclip that will load. Below is the code I have so far:

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

有没有办法从数组做出了独特的可变(不管它是什么),这样我可以命名后,一个影片剪辑,因此将加载新的影片剪辑?谢谢

Is there a way to make a unique variable (whatever it is) from the array so that I can name a movieclip after it so it will load the new movieclip? Thanks

推荐答案

什么是你的菜单列表阵列组成的?字符串?引用影片剪辑?还是其他什么东西?我会认为这是一个字符串数组。

What is your "menuList" Array made up of? Strings? References to MovieClips? Or something else? I will assume it is an Array of Strings.

记住,的addChild方法需要一个类的实例一类,而不是名字。

Remember, the addChild method takes an instance of a Class, not the name of a Class.

我不知道我理解你正在尝试做的,但我相信你正试图使一个类的实例,你真的不知道(你需要生成名的基础上哪个按钮的名称被点击)。我可能会做这样的事情:

I am not sure I understand what you are trying to do, but I assume you are trying to make an instance of a Class that you don't really know the name of (you need to generate the name based on what button was clicked). I would probably do something like this:

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

这是什么做的是产生你所需要的类的名称,并将其存储在类名的变量。然后给它返回一个类的名称getDefinitionByName。然后,我们创建这个类的一个实例(framevar),并将其强制转换为一个影片剪辑。然后,我们这个新的影片剪辑添加到根目录下。

What this is doing is generating the name of the Class that you need, and storing it in the className variable. Then giving the name to getDefinitionByName which returns a Class. We then create an instance (framevar) of that class and typecast it to a MovieClip. We then add this new MovieClip to root.