mercredi 19 avril 2017

AS3: Access variable outside of function

This issue is driving me nuts and I don't know what I'm missing here. This is my code:

package  {
        import flash.display.*;
        import flash.text.*;
        import flash.xml.*; 
        public class PopUp extends Sprite {
                public var container:Array = new Array();

                public function PopUp() {
                        var contentXML:XML = new XML(); 
                        var XML_URL:String = "./content/content.xml"; 
                        var contentXMLURL:URLRequest = new URLRequest(XML_URL); 
                        var contentLoader:URLLoader = new URLLoader(contentXMLURL); 
                        contentLoader.addEventListener("complete", FuncXML);
                        function FuncXML(event:Event):void { 
                                contentXML = XML(;
                                for each (var item in contentXML.item) {
                                        var txtbox:Sprite = new Sprite();
                              , 0.9);
                              , 0, 100, 100, 15, 15)
                               = "txtbox_";
                                trace(container.length); // Returns 2
                trace(container.length); // Returns 0


The final version of this code should create text boxes from the contents of an xml file, but I can't get the content out of the function "FuncXML". I tried this with trace. The trace inside the function returns the correct length of "container", which is 2. Outside the function, the trace returns 0. Could you please tell me what's wrong here?

Aucun commentaire:

Enregistrer un commentaire