views:

192

answers:

1

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

A: 

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

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

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.

TandemAdam
These are the following errors I get:1120: Access of undefined property i. / 1118: Implicit coercion of a value with static type Object to a possibly unrelated type Class. which is referring to the 3rd line of code, frameVarClass
steve
I thought it was obvious that the menuList references MovieClips because I said an example item is "menu_bag_mc" and said that when you click on each array item, it should link to a "movieclip that will load." I'll be more specific next time, though.
steve
I just updated that line. It should work now. I was just wondering what types menuList contains because you say menuList[i] += "_frame". You can not add a String to a MovieClip.
TandemAdam
I'm still getting "access of undefined property i"....
steve
To test it out I put `0` in place of `i` and I get this: ReferenceError: Error #1065: Variable [object menu_bag_mc_79]_frame is not defined. at global/flash.utils::getDefinitionByName() at site2_fla::menu_mc_23/frame1()
steve
It is because you are trying to add a String to a MovieClip like I said! MovieClip has a "toString" method that is called when you try and use one as a String. This method returns something like "[object MovieClip]" or "[object menu_bag_mc_79]" in your case. so you are concatenating that String and "_frame" to get the String "[object menu_bag_mc_79]_frame"
TandemAdam