简体   繁体   English

未保存在加载功能之外的数组-ActionScript 3和XML

[英]Arrays not saved outside of the load function - ActionScript 3 and XML

First of all, I'm really sorry I'm asking this again but after reading through the top 3 answers to this problem, none seemed to have given me any resolution. 首先,非常抱歉,我再次提出这个问题,但是在仔细阅读了该问题的前3个答案之后,似乎没人给我任何解决方案。

I'm trying to load in an XML file containing level data into my AS3 game. 我正在尝试将包含关卡数据的XML文件加载到AS3游戏中。 It's quite simple, the XML is dumped into an array that I've checked through and through - the length, size, and each and every variable inside of it is a-okay. 这很简单,XML被转储到我检查过的数组中-长度,大小以及其中的每个变量都是可以的。

The only real problem is that my array gets purged whenever I refer to it outside of the function. 唯一真正的问题是,只要我在函数外部引用数组,就将其清除 I've read that it's related to asynchronous nature of ActionScript and that I need a separate handler... but, perhaps I am simply an idiot, I do posses such a handler. 我已经读到它与ActionScript的异步特性有关,并且我需要一个单独的处理程序...但是,也许我只是一个白痴,所以我确实拥有这样的处理程序。

Could anyone take a peek at this here piece of code and perhaps bonk me on the head with a big stick as to what I made wrong? 有人可以窥视一下这里的这段代码,也许还可以大棒地把我弄错了吗?

package{
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;

public class LevelLoader extends MovieClip{

    private var _root:MovieClip;

    public var lvlArray:Array = new Array(); //Tablica (wektor) trzymający nam ułożenie mapy.

    private var xmlFile:XML;// The XML File Holder, duuh.
    private var urlLoader:URLLoader = new URLLoader();//The XML File Loader, duuuh.

    private var variant:int;

    public function LevelLoader(v:int){
        loadXML();
        variant = v;
        trace('Poza: '+lvlArray.length);
    }

    // Dośc vokalidów. Ale przeklejam z starego kodu.
    private function loadXML(file:String = "XMLG.xml"):void
    {
        urlLoader.addEventListener(Event.COMPLETE, parseXML);
        urlLoader.load(new URLRequest(file));
    }

    private function parseXML(e:Event):void
    {
        xmlFile = new XML(e.target.data);
        loadArray(variant,xmlFile);
    }

    private function loadArray(vars:int,xmlInput:XML):void
    {
        //trace(xmlInput.lay[vars].children().length());
        var layLength:int = xmlInput.lay[vars].children().length();
        for (var i:int = 0; i<layLength; i++){
            this.lvlArray.push(xmlInput.lay[vars].field[i].@type);
            //trace(xmlInput.lay[vars].field[i].@type);
             }
        saveArray(lvlArray);
        trace('W: '+lvlArray.length);
    }

    private function saveArray(arr:Array):void{
        lvlArray = arr;
    }

}

} }

PS Yes those are comments in mixed Polish and English. 附言:是的,这些是波兰语和英语混合注释。 Please, make nothing of it. 请不要做任何事。

I don't think your array is getting "purged," I think it's possible that you're attempting to reference the array before parseXML has been called. 我不认为您的数组被“清除”,我认为您可能在调用parseXML之前尝试引用该数组。

Since you've only posted the code for LevelLoader , I'll assume that somewhere else, you're attempting to reference LevelLoader.lvlArray . 由于您只发布了LevelLoader的代码, LevelLoader我假设在其他地方,您尝试引用LevelLoader.lvlArray You should hold off on this action until you've received some sort of "complete" or "loaded" event that you will dispatch from your loadArray method (there's no real need for your saveArray method): 您应该推迟执行此操作,直到收到将从loadArray方法分派的某种“完成”或“加载”事件loadArray (不需要saveArray方法):

package{
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.display.MovieClip;

    public class LevelLoader extends MovieClip{

        private var _root:MovieClip;

        public var lvlArray:Array = new Array(); //Tablica (wektor) trzymajacy nam ulozenie mapy.

        private var xmlFile:XML;// The XML File Holder, duuh.
        private var urlLoader:URLLoader = new URLLoader();//The XML File Loader, duuuh.

        private var variant:int;

        public function LevelLoader(v:int){
            variant = v;
            trace('Poza: '+lvlArray.length);
        }

        // Dosc vokalidów. Ale przeklejam z starego kodu.
        public function loadXML(file:String = "XMLG.xml"):void
        {
            urlLoader.addEventListener(Event.COMPLETE, parseXML);
            urlLoader.load(new URLRequest(file));
        }

        private function parseXML(e:Event):void
        {
            xmlFile = new XML(e.target.data);
            loadArray(variant,xmlFile);
        }

        private function loadArray(vars:int,xmlInput:XML):void
        {
            //trace(xmlInput.lay[vars].children().length());
            var layLength:int = xmlInput.lay[vars].children().length();
            for (var i:int = 0; i<layLength; i++){
                this.lvlArray.push(xmlInput.lay[vars].field[i].@type);
                //trace(xmlInput.lay[vars].field[i].@type);
            }
            trace('W: '+lvlArray.length);

            dispatchEvent(new Event(Event.COMPLETE));
        }

    }
}

Wherever you're instantiating LevelLoader , you'll want to listen for this event: 无论在何处实例化LevelLoader ,您都想监听此事件:

var lvlLoader:LevelLoader = new LevelLoader(1);
lvlLoader.addEventListener(Event.COMPLETE, onLevelLoaded);
lvlLoader.loadXML();

private function onLevelLoaded(e:Event):void {
    var lvlLoader:LevelLoader = e.target as LevelLoader;
    lvlLoader.removeEventListener(Event.COMPLETE, onLevelLoaded);

    // you may NOW reference lvlLoader.lvlArray
    trace(lvlLoader.lvlArray.length);
}

Note that I've made the loadXML method public so that it may be called after you've added the event listener. 请注意,我已经将loadXML方法公开,以便在添加事件侦听器之后可以调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM