views:

55

answers:

3

i can't read the variable in the function, i want to use it outside the function, here is my code.

var contentLoader:URLLoader = new URLLoader(); contentLoader.load(new URLRequest("http://localhost/data.php"));

function onComplete(event:Event):void {

var txtu:String = event.target.data;

} contentLoader.addEventListener(Event.COMPLETE, onComplete);

trace(txtu);

thanks.

A: 

basically you have a variable that is declared local to the function currently. you'll have to declare the variable outside of the function, where your contentLoader variable is defined and then assign the value in the function.

nathan gonzalez
i tried that too, yet not working, here is the code again.var txtu:String;var contentLoader:URLLoader = new URLLoader();contentLoader.load(new URLRequest("http://localhost/flash/playerdata.php"));function onComplete(event:Event):void{ txtu = event.target.data; }contentLoader.addEventListener(Event.COMPLETE, onComplete);trace(txtu);if u show me an example itz nice, thanks 4 the replay.
kaym
are you sure data is being returned by the urlloader? have you tried tracing the data inside the function? also, try casting the event.target as a urlloader explicitly like var loader:URLLoader = URLLoader(event.target);
nathan gonzalez
yea i did that i traced txtu inside the function and its working, but when i call it outside the function its empty.
kaym
please re edit your code to make the changes in the original question it is hard to read your code in these comments.
phwd
have you tried adding the event registration before the call to contentLoader.load? seems like the content could possibly be loaded before hitting the line that registers the event
nathan gonzalez
A: 

The issue here is that trace(txtu) is executed immediately after contentLoader.addEventListener(Event.COMPLETE, onComplete), which means it is occurring before the URLLoader is finished loading. So, there is nothing to trace in this situation because it hasn't been loaded yet.

Try calling another function at the end of onComplete(), which will ensure that the external data has fully loaded by that point.

For example:

var contentLoader:URLLoader = new URLLoader();
contentLoader.load(new URLRequest("http://localhost/data.php"));

function onComplete(event:Event):void 
{
  var txtu:String = event.target.data;
  continueWithProgram();
} 

contentLoader.addEventListener(Event.COMPLETE, onComplete);

function continueWithProgram():void
{
  trace(txtu);
}
letseatfood
A: 

you should be able to solve the problem by either passing the result into a new method like this:

var loaderResult: String;

var contentLoader:URLLoader = new URLLoader();
    contentLoader.addEventListener(Event.COMPLETE, onComplete);
    contentLoader.load(new URLRequest("http://localhost/data.php"));


// #option 1

function onComplete (event:Event): void {
    var txtu:String = event.target.data;
    continueWithProgram(txtu);
} 

function continueWithProgram (value:String): void {
  trace(txtu);
}

or use a variable outside of the event handler:

// #option 2

var loaderResult: String;

function onComplete (event:Event): void {
    loaderResult = event.target.data;
    continueWithProgram();
}

function continueWithProgram (): void {
  trace(loaderResult);
}

hope i could shed some light.. ;) regards.

the binary