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.events.*;
        import flash.net.*;
        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(contentLoader.data);
                                for each (var item in contentXML.item) {
                                        
                                        var txtbox:Sprite = new Sprite();
                                        txtbox.graphics.beginFill(0x444444, 0.9);
                                        txtbox.graphics.drawRoundRect(0, 0, 100, 100, 15, 15)
                                        txtbox.graphics.endFill();
                                        txtbox.name = "txtbox_"+item.settings.target;
                                        addChild(txtbox);
                                        container.push(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