RADXack
RADXack

Reputation: 1092

AS3: Upon Loading Image Return Image

Currently I have a function (LoadImg) that will load the given file location and return the loader. It's good practice to also include what amounts to 'when the image has actually COMPLETED loading' line in. This is usually done with an event handler. But with such you can only say once the image has loaded run another function. But I need the 'LoadImg' function to return the image.

function LoadImg(Img:String):Loader
{
var myLoader:Loader;

myLoader=new Loader();
myLoader.load(new URLRequest(Img));

return myLoader;
}

How can I say only return 'myLoader' when it's completed?

Upvotes: 1

Views: 119

Answers (2)

Daniel
Daniel

Reputation: 35684

I think the solution is to use a Proxy.

The idea is that the proxy, which is in this case an image container. The container can be passed back and used by other functions and objects to set location. Once the image actually loads, the load complete handler can update the container with the loaded image.

You can read up about the proxy design pattern at http://www.as3dp.com/category/design-patterns/proxy/ with code examples and detailed description.

Upvotes: 0

Michael
Michael

Reputation: 325

I don't think its possible to use a synchronous call (function return) with an asynchronous event (loader complete).

Attaching an event listener to the Complete event is the way to go, not sure how that will fit into your code.

Here is a similar question which might be useful: Image size in AS3, the response is useful to your question.

Upvotes: 1

Related Questions