views:

76

answers:

1

Continued from here: http://stackoverflow.com/questions/2906847/create-variable-from-array-actionscript-3

I'm usually not this rushed, but I have to have this project finished by tonight... so any help is appreciated. The poster there gave me the following code, which gets "access of undefined property i" on the second line. All the items in the list are movie clips, and I have a second movieclip in the library with the same name, but has "_frame" appended to it (menu_bag_mc_frame), and for every time that each corresponding array item is clicked, I need to create a variable item with the same name and _frame appended to the end.

var menuList:Array = [menu_bag_mc,menu_chips_mc,menu_coke_mc];
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);

Why do I get an undefined property?

A: 

Nothing in that code is defining the i variable.

In order to be accessed, a variable needs to be both declared. e.g:

var i:int;

and defined e.g:

i = 1;

sometimes these are done together:

var i:int = 1;

i is commonly used as an index variable in a loop, so you might have something like:

var menuList:Array = [menu_bag_mc,menu_chips_mc,menu_coke_mc];

for(var i:int = 0; i<menuList.length; i++)
{
    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);

}

which declares and defines the i variable in the for loop, so that the code will execute for each index in the menuList array.

Edit: if I understand the intent of the original code properly, the array should be of string class names rather than the classes themselves:

var menuList:Array = ["menu_bag_mc","menu_chips_mc","menu_coke_mc"];
DanK
Okay, I understand.I used this code, but now it returns: 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
I think I see the problem - the original code that you posted does not put the class names in speech marks in the array. If I understand the intent of the code properly it should be ["menu_bag_mc","menu_chips_mc","menu_coke_mc"]
DanK
I put them in quotes, and now it returns this: TypeError: Error #1034: Type Coercion failed: cannot convert "menu_coke_mc" to flash.display.MovieClip. at site2_fla::menu_mc_23/frame1() . I'm beginning to think it's not possible, but thanks for all your help.
steve