davidpadua
12/3/2013 - 1:00 AM

Get definition by name

Get definition by name

//INSTEAD OF THIS:
////////////////////////////////////////////////////
private function createTiles():void
{
    var tile1:Tile1 = new Tile1();
    var tile2:Tile2 = new Tile2();
    var tile3:Tile3 = new Tile3();
    var tile4:Tile4 = new Tile4();
    var tile5:Tile5 = new Tile5();
    var tile6:Tile6 = new Tile6();
    var tile7:Tile7 = new Tile7();
    stage.addChild( tile1 );
    stage.addChild( tile2 );
    stage.addChild( tile3 );
    // You get the idea, it is very lengthy!
}

///////////////////////////////////////////////////////
//DO THIS:

Tile1;Tile2;Tile3;Tile4;Tile5;Tile6;Tile7;
//etc

private function createTiles():void{

    for( var i:int = 1; i < 8; i++ ){
 
        var tileRef:Class = getDefinitionByName( "Tile" + i ) as Class;
        var tile:Sprite = new tileRef();
        stage.addChild( tile );
 
    }
}