2

So my problem is that trace within the function does trace the first element of the array, but the trace outside if the function does not. I do declare the array variable outside the function, but the data wont save to the array variabel.

var oppgaveLoader:URLLoader = new URLLoader();

oppgaveLoader.load(new URLRequest("oppgaver.txt"));
var oppgaveNr = 0
//store line of text on an array called oppgaver
var oppgaver:Array = []
var oppg:Array = new Array()
oppgaveLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event){
    oppgaver = e.target.data.split(/\n/)
    trace(oppgaver[0]) //This one traces the frist item in the array
}
trace(oppgaver[0])//This one does not trace the first one in the array

Does anyone know why and/or how to fix it if posible? The file "oppgaver.txt" is located in the same directory as my .fla file

The file "oppgaver.txt" is laid out like this (the text is in norwegian, but each line is going to be a item in the array):

Hvor gjelder forbudsskilt hvis ikke annet er oppgitt?
Hvordan foretar du best mulig bremsing og unnastyring?
Hvordan bør du normalt plassere bilen på en vanlig 2-felst vei?
VC.One
  • 14,790
  • 4
  • 25
  • 57

1 Answers1

1

It's a synchronicity problem.

the last trace happens immediately after you set up your arrays, but those arrays are still empty.

only when the onLoaded function gets called, asynchronously by the URLLoader, they get populated and you can trace their values.

that event listener basically lets you react to an event that happens in the future at some point.

alebianco
  • 2,475
  • 18
  • 25