开发者

How to process events chain

I need to process this chain using one LoadXML method and one urlLoader object:

ResourceLoader.Instance.LoadXML("Config.xml");
ResourceLoader.Instance.LoadXML("GraphicsSet.xml");

Loader starts loading after first frameFunc iteration (why?) I want it to start immediatly.(optional)

And it starts loading only "GraphicsSet.xml"

Loader class LoadXml method:

    public function LoadXML(URL:String):XML
    {
        urlLoader.addEventListener(Event.COMPLETE,XmlLoadCompleteListener);
        urlLoader.load(new URLRequest(URL));                
        return xml;
    }
    private function XmlLoadCompleteListener(e:Event):void
    {
        var xml:XML = new XML(e.target.data);
        trace(xml);
        trace(xml.name());
        if(xml.name() == "Config")
                XMLParser.Instance.GameSetup(xml);
        else if(xml.name() == "GraphicsSet")
                XMLParser.Instance.GraphicsPoolSetup(xml);
    }

Here is main:

        public function Main()
    {
        Mouse.hide();
        this.addChild(Game.Instance);
        this.addEventListener(Event.ENTER_FRAME,Game.Instance.Loop);            
    }

And on adding a Game.Instance to the rendering queue in game constuctor i start initialize method:

        public function Game():void
    {
        trace("Constructor");
        if(_instance)
            throw new Error("Use Instance Field");
        Initialize();           
    }

its code is:

private function Initialize():void
    {
        trace("initialization");
        ResourceLoader.Instan开发者_高级运维ce.LoadXML("Config.xml");
        ResourceLoader.Instance.LoadXML("GraphicsSet.xml");             
    }

Thanks.


Your code is a bit confusing to read because you don't follow the accepted naming conventions for AS3.

Methods & variables names should not be capitalized , rather camelCased:

  private function xmlLoadCompleteListener(e:Event):void{}

Secondly , why do use Singletons? The use of Singletons is fairly controversial, do a little search on this site to find out why.

In your particular case, your loading problem comes from the way you're using your Singleton. If you need to load several files concurrently, you will need to create a new URLLoader instance for each operation , otherwise you will only load the last operation called.

    private var assets:Array = ['Config.xml' , 'GraphicsSet.xml'];

    //Load your assets
    private function loadAssets( value:Array ):void
    {
        for each ( var url:String in value )
        {
           var urlLoader:URLLoader = new URLLoader();
           urlLoader.addEventListener(Event.COMPLETE, xmlLoadCompleteListener);
           urlLoader.load(new URLRequest(URL)); 
        }
     }


Since you haven't provided any information on where exactly those two lines containing the LoadXML statements are located, I can't answer the part about why they are not loading immediately.

As for the other part: It appears you are using a singleton instance for loading. Since you don't allow it to finish the first load operation, the second load operation will override the first one immediately, and your program will never load the requested file. You can get around this by either using two ResourceLoader instances, or starting the second operation in your XMLLoadCompleteListener, after the first file has finished loading.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜