Vlad
Vlad

Reputation: 85

functions and variables

Well the question is quite simple but I somehow can't figure it out.

For example I have string variable ts. After my loader loads image in movieclip instance i want to change ts value to something. The problem is - ts value isn't changing on my doneLoad function. Here's the code

var ts:String = "loading";   

var imgload = new Loader();

imgload.load(new URLRequest("http://images.op.com/cards/up1x3941204.jpg"));

imgload.contentLoaderInfo.addEventListener(Event.COMPLETE,doneLoad);

function doneLoad(e:Event):void {
 ts = "done";
}

trace(ts); // returns "loading"

What's the problem?

Upvotes: 0

Views: 122

Answers (1)

Justin Niessner
Justin Niessner

Reputation: 245399

Your problem is that trace(ds) is called before the function doneLoad ever runs.

doneLoad is a callback function and doesn't run until ofater the Loader completes. Your call to trace(ds) is outside the callback and therefor runs as soon as the app starts (or whenever the rest of the code runs). Hence, when trace is called...the value is still "loading".

Change your code to:

var ts:String = "loading";
var imgUpload:Loader = new Loader();

imgload.load(new URLRequest("http://images.op.com/cards/up1x3941204.jpg"));
imgload.contentLoaderInfo.addEventListener(Event.COMPLETE,doneLoad);

function doneLoad(e:Event):void
{ 
    ts = "done";
    trace(ds);
}

Upvotes: 1

Related Questions