# AS3 dynamic symbol instantiation - best method?

**URL:** <https://forum.kirupa.com/t/as3-dynamic-symbol-instantiation-best-method/255456>\
**Category:** flash\
**Created:** [March 22, 2008, 10:58pm UTC](https://forum.kirupa.com/t/as3-dynamic-symbol-instantiation-best-method/255456 "2008-03-22T22:58:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickZA](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nickza/32/3452_2.png) [@NickZA](https://forum.kirupa.com/u/NickZA)\
**Post date:** [March 22, 2008, 10:58pm UTC](https://forum.kirupa.com/t/as3-dynamic-symbol-instantiation-best-method/255456/1 "2008-03-22T22:58:46Z")

</div>

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…

```auto

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…

```auto

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? 😉

-Nick
