Jump to content
Search Community

Check for Complete of Prioritized Loader?

Nickbee test
Moderator Tag

Recommended Posts

I know there must be an easy way of doing this.

 

I’m pre-loading my main images like so…

		private function loadMainImages():void
	{
		for(var i:int=0 ; i<_numThumbs ; ++i)
		{
			myLoaderMax.append(new ImageLoader(_myXML.images.mainURL[i], {name:"main" + i}));
			_mainLoaderNames.push("main" + i);
		}			
	}

 

On my thumbnail click I'm calling a prioritize function like so...

 

		private function onThumbClick(evt:MouseEvent):void
	{
		//find which index of _buttons Array was clicked
		for (var i:uint=0 ; i<_numThumbs ; i++)
		{
			if (_buttons[i].name == evt.target.parent.name)
			{
				break;
			}
		}
		prioritizeLoader(i);
	}

 

The idea is to have the prioritizeLoader method prioritize the main image that is needed then use it as an argument in the newGame method.

 

I came up with this:

 

		private function prioritizeLoader(indexToLoad:uint):void
	{
		LoaderMax.getLoader(_mainLoaderNames[indexToLoad]).prioritize();

		_game.newGame(LoaderMax.getLoader(_mainLoaderNames[indexToLoad]).rawContent, 1);

	}

 

Which obviously only works if the main image in question is loaded and ready to go. I'd like to add an "onComplete" to prioritized loader but not sure how to.

 

Thanks!!!!

Link to comment
Share on other sites

To answer your question, you can add a listener the "standard" way just like any other AS3 class, like:

 

myLoader.addEventListener(LoaderEvent.COMPLETE, completeHandler);

 

However, I think you might be overcomplicating things a bit. First of all, are you aware that ImageLoader immediately creates a ContentDisplay object into which it loads the rawContent? So you can use the "content" right away (before anything even loads). This makes it much more convenient than having to wait for things to load. And you can set a width/height ahead of time and it will honor that.

 

Also, have you thought of using a Dictionary as a lookup table to associate your loaders with your thumbnails? Actually, I'd probably structure things a bit differently and use a custom class for my thumbnails so that I could define a "mainLoader" property for each one and handle the rollover/out/click stuff internally, but that's beyond the scope of what you asked here. Anyway, you could use a Dictionary so that you don't have to store all the names in an array and loop through them each time:

 

var thumbLookup:Dictionary = new Dictionary();

//then link each thumbnail to the loader like:
thumbLookup[thumbnailSprite] = new ImageLoader("yourURL.jpg");
...

//then in your MouseEvent listener, get the loader for the associated thumbnail like:
function clickHandler(event:MouseEvent):void {
   var loader:ImageLoader = thumbLookup[event.currentTarget];
   loader.prioritize();
    _game.newGame(loader.content, 1);
}

Link to comment
Share on other sites

I should have been more clear on what I'm going to accomplish in the application (game)...

 

This is a slice an image up and slide the pieces back together game as seen in this prototype...

http://www.nickbee.com/away3d16

 

It will be important for the image to be be loaded before going over to my game class for slicing and dicing using the image's bitmapData.

 

I'm planning to have 10-15 images for the final game. The thumbs load at hit the stage ASAP then all the main images preload while you play the 1st random image. If someone clicks a thumbnail immediately there is a chance the main image won't be preloaded as of yet. So the idea is to have that image prioritized and to detect when that particular image is complete so it can be sent over to the game class.

 

So back to the code for my prioritize function. It now looks like this:

 

		private function prioritizeLoader(indexToLoad:uint):void
	{
		myLoaderMax.getLoader(_mainLoaderNames[indexToLoad]).prioritize();
		myLoaderMax.getLoader(_mainLoaderNames[indexToLoad]).addEventListener(LoaderEvent.COMPLETE, completeHandler);
	}

 

The completeHandler is not firing, however this is publishing fine.

 

And being that I'm an AS3 novice I'm sure I'm making this more complicated than it needs to be :)

 

Thanks again for all your help!!!!

Link to comment
Share on other sites

It sounds like maybe the loader that you're prioritizing has already finished loading at that time - that would explain why the COMPLETE event wouldn't fire after that point. I'd recommend adding some conditional logic like this:

 

private function prioritizeLoader(indexToLoad:uint):void {
   var loader:ImageLoader = LoaderMax.getLoader(_mainLoaderNames[indexToLoad]);
   if (loader.status == LoaderStatus.COMPLETED) {
       //do stuff immediately...
   } else {
       loader.addEventListener(LoaderEvent.COMPLETE, completeHandler);
       loader.prioritize();
   }
}

Link to comment
Share on other sites

When in doubt use a conditional :)...

 

One quick correction just in case someone is following this...

 

if (loader.status == LoaderStatus.COMPLETE) {

 

should be

 

if (loader.status == LoaderStatus.COMPLETED) {

 

This code now works EXACTLY as I imagined. Next I will add a loader status if the image needs to load.

 

THANKS!!!!!!!!!!!!!!!!!

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...