vendredi 19 février 2016

How to get values for buttons created with (sprite) class

In the last line, I want the XML function to read the name of the button clicked and show the corresponding tag in the XML file (the tags in the XML file has the same names of the buttons), or instead I will have to make functions or if statements equal to the number of buttons I have! this does not make any sense to me!

stop();
import flash.events.MouseEvent;
import flash.events.Event;
import flash.net.URLLoader;
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;

var myVar:String = new String();
//var myFrame:Array=new Array(26);
var myDictionary:Array = new Array('Amon','Ahmos','Ramsis');//the words

for (var j:int=0; j<myDictionary.length; j++)
{
    var word:MovieClip = getTextButton2(myDictionary[j]);//loop in myDictionary and name each button with a different name
    addChild(word);
    word.x = 130;
    word.y = 16 + (j * 30);// to change the position of each word

    word.addEventListener(MouseEvent.CLICK,Vocabulary);

    function getTextButton2(label:String):MovieClip //creat buttons 
    {
        var txt:TextField = new TextField();
        txt.defaultTextFormat = new TextFormat('Calibri',20,0x000000);
        txt.text = label;
        txt.autoSize = TextFieldAutoSize.LEFT;
        txt.background = txt.border = true;
        txt.selectable = false;
        var btn:MovieClip = new MovieClip();
        btn.mouseChildren = false;
        btn.addChild(txt);
        btn.buttonMode = true;
        myVar=label.valueOf();
        trace(myVar.valueOf());
        return btn;
    }
    function Vocabulary(event:MouseEvent)
    {
        var myLoader:URLLoader= new URLLoader();
        myLoader.load(new URLRequest("1.xml"));
        myLoader.addEventListener(Event.COMPLETE,loadXML);
        function loadXML(e:Event)
        {
            var myxml:XML = new XML(e.target.data);
            mydata.text +=  String(myxml.myVar);// instead of 0 it must be a variable depends on the button pressed (the word)
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire