Jump to content
Search Community

Clear ImageLoader on IOError Event

szaqal test
Moderator Tag

Recommended Posts

Hi I've just started my adventure with ImageLoader and I've got a question about IO_ERROR event. How can I clear the loader when it dispatches LoaderEvent.IO_ERROR? Can it be done by this code:

 

private function loadLogo(imagePath:String):void
	{			
		img_loader = new ImageLoader(imagePath, {name: "logo",
															  container: logo_container,
															  x: 0,
															  y: 0,
															  width: 50,
															  height: 27,
															  scaleMode: "proportionalInside",
															  centerRegistration: true,
															  onIOError: onLogoLoadError,
															  onComplete: onLogoLoadComplete});
		img_loader.load();
	}

	private function onLogoLoadError(le:LoaderEvent):void
	{
		img_loader.dispose();
		img_loader = null;
	}

	private function onLogoLoadComplete(le:LoaderEvent):void
	{
		TweenLite.to(blackout_layer, 1, {alpha: 0});

		onLogoLoadError(null);
	}

 

I simply want to nullify img_loader in onLogoLoadError.

Link to comment
Share on other sites

Sure, when you dispose() a loader, it'll stop loading and make it eligible for garbage collection. Also, you can pass autoDispose:true into your ImageLoader (via the vars parameter) if you want it to automatically dispose of itself as soon as it completes. That doesn't delete the actual content that was loaded - it just disposes of the ImageLoader instance. Just be careful because when you dispose(), the loader can never be found again with LoaderMax.getLoader() or LoaderMax.getContent().

 

Did that answer your question?

 

If you haven't done so already, check out the video tutorials that Rich Shupe has been working on at http://www.greensock.com/loadermax-video/

Link to comment
Share on other sites

Not exactly :)autoDisposse: true fires dispose() automaticly onComplete that's clear to me. But is dispose() fired when loader dispatches IO_ERROR event or do I have to do it manually? What happenes then to the loader I mean after IO_ERROR occurs? Now I'm making it eligible for GC by this code

private function onLogoLoadError(le:LoaderEvent):void
     {
        img_loader.dispose();
        img_loader = null;
     }

is that ok? My previous question wasn't clear enough I think :).

Link to comment
Share on other sites

Sure, that's fine.

 

To answer your question, dispose() is NOT called automatically when an IO_ERROR happens, nor should it be because if it was called automatically, you'd never be able to check on its progress or status, etc. by using the commonly-used LoaderMax.getLoader() or LoaderMax.getContent() methods. If you want to dispose of the loader when an IO_ERROR occurs, you need to call dispose() yourself. Do you see why that makes sense?

 

Also keep in mind that you can define an alternateURL if you want. :)

Link to comment
Share on other sites

Yes, that makes sens and answers my question :) but I've got one more is it possible to set onComplete handler other way then in constructor vars properties. Let's say that I want to call load() several times on the same loader object but with diffrent onComplete handlers and I don't want to call the whole constructor method each time but only set the new url and new onComplete handler like:

var img_loader = new ImageLoader(imagePath, {name: "logo",
                                                 container: logo_container,
                                                 x: 0,
                                                 y: 0,
                                                 width: 50,
                                                 height: 27,
                                                 scaleMode: "proportionalInside",
                                                 centerRegistration: true,
                                                 onIOError: onLogoLoadError,
                                                 onComplete: onLogoLoadComplete});
img_loader.load();

function onLogoLoadComplete(le:LoaderEvent):void
{
       img_loader.url = "some_url";
       img_loader.onComplete = newOnCompleteHandler;
       img_loader.load(true);
}

Link to comment
Share on other sites

Absolutely - the onComplete vars special property is just a convenient shortcut for doing this:

myLoader.addEventListener(LoaderEvent.COMPLETE, myHandler);

 

LoaderMax was built with an extensive event system that uses the standard AS3 event model. So when you define an onComplete, LoaderMax will actually do the addEventListener() for you internally and then when you dispose() the loader, it will automatically removeEventListener() that as well. If you manually addEventListener() just don't forget to removeEventListener() too when appropriate.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...