Denoteone
Denoteone

Reputation: 4055

Append letters onto variable name

I need to append 4 letters to my variable name when referencing it to get either spanish or english XML data. I am trying to reuse my functions without writing an if statement.

like so:

//two options for
    var spa_my_videos:XMLList;
    var eng_my_videos:XMLList;

//APPEND OPTIONS
    var spanishLang:String = "spa_";
    var englishLang:String = "eng_";

//CALL FUNCTION AND ADD APPEND OPTION
    makePlayer (englishLang);


    function makePlayer (lang:String):void {

        my_player = new FLVPlayback();
        my_player.seekBar = SB;
        my_player.playPauseButton = PPB; 
        my_player.volumeBar = VB;

        my_player.x = video_x;
        my_player.y = video_y;
        my_player.width = 1023;
        my_player.height = 630;

        main_container.addChild (my_player);

    //THIS IS WERE DEPENDING ON THE VALUE OF LANG I NEED TO USE THE spa_ or eng_ XML data

        my_player.source = root_path + lang + my_videos[0].@URL;
    }

Upvotes: 0

Views: 106

Answers (2)

felipemaia
felipemaia

Reputation: 3571

Your question is a litle confusing, but if I understood you correctly, you can get past this by using the Object["variableName"] feature available on AS3. This feature allows you to access a variable by either Object.variable or Object["variable"], so you can build the variable name on a string, and use it to reference what you want.

Upvotes: 1

arahaya
arahaya

Reputation: 1020

outside of the makeplayer function

var videos:Object = {};
videos[spanishLang] = spa_my_videos[0].@URL;
videos[englishLang] = eng_my_videos[0].@URL;

in the makeplayer function

my_player.source = root_path + lang + videos[lang][0].@URL;

Upvotes: 0

Related Questions