从数组动作创建变量3
我目前正在尝试通过数组和循环来创建动态菜单。 所以当有人点击数组中的第一个项目“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);
}
}
有没有办法从数组中创建一个唯一的变量(无论它是什么),以便我可以在它后面命名一个动画片段,以便它将加载新的动画片段? 谢谢
你的“menuList”数组是由什么组成的? 字符串? 参考影片剪辑? 或者是其他东西? 我会认为它是一个字符串数组。
请记住,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返回一个类。 然后,我们创建该类的实例(framevar)并将其转换为MovieClip。 然后,我们将这个新的MovieClip添加到根。
链接地址: http://www.djcxy.com/p/17761.html上一篇: create variable from array actionscript 3
下一篇: Creating dynamic objects for quiz questions Flash CS4, AS3