views:

51

answers:

2

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?

A: 

To check if ts is changing correctly, you should be traceing it inside the doneLoad function, since a trace outside of that event function will most likely be called before the event is actually fired.

Matt
+1  A: 

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);
}
Justin Niessner
OMG, and what if I want to use changed variable value somwhere else? Outside event function?
Vlad
As long as the code outside of the event functions runs after the event function...you'll have the new value.
Justin Niessner
Actually it goes after event function (see code in question) but value stays old. And (can't put it in my mind - this way of, uhm, working with variables confusing me) code after event function will be executed before event function will done it's work. What to do with this issue?
Vlad
Going after and being executed after are two different things.
Justin Niessner
Is there any way to make them equal? I mean to execute code really after event function.
Vlad