AS3 dynamic symbol instantiation - best method?

Hi

I’ve just migrated to AS3 and am trying to get my head around how to instantiate library symbols as instances of a class.

Here’s the problem: I’m making a tile-based game with a Map object to which are linked many Tile objects. Now while I initially create blank tiles, I later want to populate them with terrain. This operation will change thus the tile’s _terrainType property. Finally, I want to then draw the map consisting of its many tiles. Tiles without a terrain type will remain plain vanilla, but those that have terrain types specified would obviously appear different.

Now the Tile object itself extends Sprite, so it is a DisplayObjectContainer (k, no problems there). My question is, how do I now either a) directly assign it a library symbol graphic, following it’s instantiation (if this is even possible?) or b) dynamically use addChild() to add a library symbol graphic of a certain classname (my preferred tactic)?

The problem is if I have to do something like this…


var tileImg:MovieClip = new mountainTerrain();
addChild(tileImg);

…then I have to write a bleedin’ huge switch() statement to reference a hash table which specifies which terrain type refers to which symbol class type, which is just ugly and crap.

The most logical thing to me would seem to be something like this…


var tileImg:MovieClip = new eval(classname)();
addChild(tileImg);

…but clearly I’m misguided because eval() doesn’t even exist in AS3 and I have attempted it’s equivalent with no likelihood of success.

Can someone help me here? How do I dynamically add a graphic to represent each instance of my Tile class? Am I approaching this the wrong way altogether, or do I just not have the correct keywords handy? :wink:

-Nick