Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

dynamic/unique variable names in a for loop in AS3

Explorer ,
Jun 24, 2009 Jun 24, 2009

hi all

here am trying to load xml

and make button for its length, and try to make function for each button i have

i make buttons and i give them a unique name , but i can't make function for every one

can any one help ?

here is the code

/////////////////////////////

var xmlLoader:URLLoader = new URLLoader();
var my_Arr:Array= new Array();
var arr_posX:Array =new Array();//= ["350","350","350","350"]
var arr_posY:Array=new Array();//= ["80","120","160","200"]
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.load(new URLRequest("Models.xml"));
function showXML(e:Event):void {
    XML.ignoreWhitespace = true;
    var songs:XML = new XML(e.target.data);
    //trace(songs.track.length());
    var i:Number;
    for (i=0; i < songs.track.length(); i++) {
        var mcArray:Array = new Array();
        var movieClip:MovieClip = new MovieClip();
        addChild(movieClip);
        mcArray = movieClip;
        var dd:String =movieClip.name="my_btn" +i
        my_Arr.push(songs.track.model.text());
        arr_posX.push(songs.track.posX.text());
        arr_posY.push(songs.track.posY.text());
        var outside:MovieClip = new MovieClip();
        movieClip.graphics.beginFill(0xFF0000);
        movieClip.graphics.drawRect(0, 0, 80, 20);
        movieClip.graphics.endFill();
        movieClip.x = arr_posX;
        movieClip.y = arr_posY;
        addChild(movieClip);
        trace(movieClip.name)
        movieClip.buttonMode=true;
                var outputText:TextField = new TextField();
        outputText.x = 5;
        outputText.y = 5;
        //outputText.background = true;
        outputText.autoSize = TextFieldAutoSize.LEFT;
        outputText.text = my_Arr;
        outputText.selectable=false;
        //this.addChild();
        movieClip.addChild(outputText);
        movieClip.addEventListener(MouseEvent.CLICK,go_path);
        function go_path(event:MouseEvent):void {
            trace("aaa");
        }
       
    }
}

TOPICS
ActionScript
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 24, 2009 Jun 24, 2009

What's this function need to do? Are you referring to 'go_path'?

I would suggest you take this out of your loop and put it at the same level as show_XML. Only have one function to deal with all of the buttons. Once you're inside the function you can deal specifically with whichever button called the function by examining the contents of your MouseEvent object. This contains a property called target, which represents the button that dispatched the CLICK event. From here you can work directly on this property, or if different buttons need to be dealt with differently you can use if/else or switch/case statements to achieve this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 25, 2009 Jun 25, 2009

thnx  Craig,

but i have the same Q how can i id the button to do an action

i mean i have here 4 button i want to let every button make different action , i can not name the button to make its action

got me ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 25, 2009 Jun 25, 2009
LATEST

nope. haven't got you.

why can't you give the button a name to identify it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines