Reputation: 121
Hopefully this should be a quick question. I'm having trouble accessing some movie clips in my library dynamically using an array.
Basically, my array holds a bunch of letters..
var monarray:Array =("AAACBCBCAABBC").split("");
Now, my library holds three movie clips. They have the following AS3 linkage: monsterA monsterB monsterC
What I want to do is create a new instance of an object based on where I am in the array.. For example, when monarray is at index [0], I want monsterA to be added..
This is the code I am using to try and achieve this effect:
var b = new monster[monarray[i]];
b.x = mouseX;
b.y = mouseY;
addChild(b);
i ++;
However, upon compiling my .swf, I get this error:
Scene 1, Layer 'Layer 1', Frame 1, Line 9 1086: Syntax error: expecting semicolon before leftbracket.
I understand this is because I'm obviously not doing it right, but I've only ever used arrays to call up a specifically-indexed instance.
I'm sorry if this is noobish, but I'm not quite sure how I can dynamically choose between these three library instances on the go!
Thanks a lot in advance!
Harry.
Upvotes: 0
Views: 1742
Reputation: 26841
Even though the answer above is correct by definition, it is still a "hacky" way of achieving what you are trying to do. I would not suggest you to use the above method unless you want a method to dynamically add new type of monsters. Let me try to explain.
Use the above method if you want to plug-in new monster types at run time.
You have your base.swf which loads an external.swf ( which contains the definitions for the monsters you mentioned! ). base.swf is already deployed and you dont want to make changes to it then the above method is good enough.
Suppose say, you already know the different types of monsters you are going to deploy at run time ( which is the case in most games! ) I would suggest you to create a factory class, use inhertitance ( create a IMonster interface ! ) and the factory class can take your A,B,C etc as input to chug out new monsters.
Upvotes: 1
Reputation: 15338
not sure try:
var mst = getDefinitionByName("monster"+monarray[i]) as Class;
var b = new mst;
Upvotes: 0