数组数据在加载器功能之外丢失

时间:2011-11-04 14:20:36

标签: arrays flash actionscript-3 variables for-loop

尝试制作基于网格的问题答案游戏,并使用for循环将数据从文本文件加载到数组中。我想减少冗余代码并使用for循环来填充我的数组。我遇到的问题是我的数组在加载器函数之外被丢失/销毁。

据我所知,如果在函数内部声明了一个数组,那么在退出该函数时,该数组自然会被销毁。但是我在函数之外声明了数组,因此在退出函数后数组应该保持不变。

我在这里缺少什么?

BTW - 我没有加载数据的问题。数据正确加载。数据加载后,数组就被简单地销毁了。

//Create the arrays to hold the 
//categories, questions, and answers
var categoryArray:Array = new Array();      
var quesAnswArray:Array = new Array(); 

//create the loader
var loader:URLLoader = new URLLoader();

//telling the loader that we are dealing with variables here.  
loader.dataFormat = URLLoaderDataFormat.VARIABLES;  

//This is an eventlistener, these are used all the time in AS3  
//learn to use them, this basically tells flash to listen to a specific event  
//and then call a specific function.  
//in Our case we listen for the event called COMPLETE which means it will active  
//a function called "loading" when our flash movie has completed loading  
loader.addEventListener(Event.COMPLETE, loading);  

//Here we tell our loading which file to read our categories and questions from.  
loader.load(new URLRequest("questions.txt"));  

//This is the function that will happen when the eventlistener activates.  
//basically it says that our text fields called content_1 and _2's text property  
//should be equal to loader.data.var_1 and var_2 (as you might remember from the     explanation above).  
function loading (event:Event):void{  

     //loading bar
     var total:Number = this.stage.loaderInfo.bytesTotal;
     var loaded:Number = this.stage.loaderInfo.bytesLoaded;

     bar_mc.scaleX = loaded/total;
     loader_txt.text = Math.floor((loaded/total)*100)+ "%";

    var varText:URLVariables = new URLVariables(event.target.data);
    //populate the category array with the value from the file.
    for (var category:int=0; category<13; category++)
        {
            this["cat"+category] = varText["var_" + [category+1]];
            categoryArray.push(this["cat"+category]);
        }
     //populate the two demensional array of questions and answers. 
         //columns
     for (var cols:int=0; cols<12; cols++){
            //rows
            for (var rows:int=0; rows<5; rows++){
                 this["q"+rows] = varText["var_c"+[cols+1]+"q"+[rows+1]];
                 this["a"+rows] = varText["var_c"+[cols+1]+"a"+[rows+1]];
                 quesAnswArray.push(new Array());
                 quesAnswArray[cols].push(this["q"+rows]);
                 quesAnswArray[cols].push(this["a"+rows]);
            }
        }
    //bonus round q&a
    this["finalQ"] = varText["var_c13q1"];
    this["finalA"] = varText["var_c13a1"];
    quesAnswArray.push(this["finalQ"]);
    quesAnswArray.push(this["finalA"]);

   if (total == loaded){
     play();
     this.removeEventListener(Event.ENTER_FRAME, loading);
   }

   trace(categoryArray); // this traces the array values perfectly
   trace(categoryArray.length); // this returns 13 which is correct
}

trace(categoryArray); //this gives me nothing. 
trace(categoryArray.length) //gives me 0

1 个答案:

答案 0 :(得分:1)

您的代码会执行它应该执行的操作。发生的事情是,一旦Flash收到数据,就会异步调用loading()函数。所以基本上你的最后一个跟踪语句在loading()之前和数组初始化之前执行。

因此,请确保在调用loading()后访问数组。也许创建一个包含初始化代码的initialize()函数。并在loading()结束时调用此函数。