0

Trying to make a grid based question answer game and load variables from a text file using for loops into an array. I wanted to reduce redundant code and use for loops to populate my arrays. The issue I am having is with my arrays being lost/destroyed outside of the loader function.

I understand that if an array is declared inside the function, naturally the array would be destroyed upon exit of the function. However I am declaring the arrays outside of the function so the arrays should remain intact after I exit the function.

What am I missing here?

BTW - I'm not having an issue with the loading of the data. The data is loading correctly. The array is simply being destroyed after the data has loaded.

//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 Answer 1

1

Your code does what it should do. What's happening is that your loading() function is called asynchronously, once the data has been received by Flash. So basically your last trace statements are executed before loading(), and before the arrays are initialized.

So just make sure you access the arrays after loading() has been called. Perhaps create a function such as initialize() which would contain your initialization code. And call this function at the end of loading().

1
  • That was it. I just needed to let flash have time to initialize my arrays. Once I did that it worked like a charm.
    – Brownarola
    Nov 8, 2011 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.