Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
alkrantz

Clarification

Recommended Posts

Hey guys,

New user to loaderMax looking for some clarification on the high level concepts of the class.

 

I have a website with a flash banner at the top. The content of the banner can be changed using 4 buttons directly below it. Each button loads a new .swf into the banner. It does not unload the previous content in the event a user wants to return to it.

 

From what i have read on this site so far, I need to create a que of loaders to load my .swfs first, and then in the event handler for each button, prioritize() the associated loader for that button. is that correct?

 

Just trying to get my head around it. Thanks in advance for any help.

-A

Link to comment
Share on other sites

You have lots of options actually. You can use SWFLoaders independently, creating one for each of your swfs and then just load() whichever one you want. Or you can put them all into a LoaderMax which is like a queue that manages loading of its children for you. The nice thing about that is you can set up the queue and let 'er rip. It'll load the children sequentially. Actually, by default it will load 2 at a time in order to maximize the use of the bandwidth and speed up the overall load of the whole queue. But you can set the maxConnections in that LoaderMax instance to 1 if you want it to load one at a time in sequence.

 

When you prioritize() a loader, it will automatically start loading it and temporarily shift it to the top of any LoaderMax instances to which it belongs. So if your LoaderMax has 4 loaders and the first one is currently loading and then you prioritize() the 4th one, it'll automatically stop loading the first one that's in progress and load the 4th one instead. Then when that one is done, it'll resume loading the rest of the LoaderMax children.

 

Make sense?

 

So yes, in your case it probably is best to put 'em into a LoaderMax queue to get them loading and then prioritize() whichever one you need at any given time. Hopefully by the time you need one, it'll already have preloaded and it'll be available almost instantly.

Link to comment
Share on other sites

From your description, something like:

 

//... setup ... //
var _queue = new LoaderMax({name:"frameLoader", onComplete:QueueComplete, onProgress:QueueProgress, onError:QueueError});
_queue.append(new SWFLoader("nameOfSwf1.swf", {name:"menu1", onComplete:Menu1Complete, estimatedSize:20000});
_queue.append(new SWFLoader("nameOfSwf2.swf", {name:"menu2", onComplete:Menu2Complete, estimatedSize:20000});
_queue.append(new SWFLoader("nameOfSwf3.swf", {name:"menu3", onComplete:Menu3Complete, estimatedSize:20000});
_queue.append(new SWFLoader("nameOfSwf4.swf", {name:"menu4", onComplete:Menu4Complete, estimatedSize:20000});

//... button press ... //
_queue.prioritize("menu1");

//... event handler functions ... //
addChild(_queue.getLoader("menu1").content);

 

Edit: Fixed example code, adding in "nameOfSwf.swf" properties

Link to comment
Share on other sites

yeah, that's pretty much the gist of it BUT you also need to specify the URL of the swf

 

_queue.append(new SWFLoader("nameOfSwf.swf", {name:"menu1", onComplete:Menu1Complete, estimatedSize:20000});

 

 

instead of adding the swf to the display list in the onComplete handlers, you CAN specify a container that the loaded swf will automatically get placed into. its a really handy feature:

 

 

_queue.append(new SWFLoader("nameOfSwf.swf", {name:"menu1", container:menu1Holder_mc, onComplete:Menu1Complete, estimatedSize:20000});

 

//in this case you would only need the onComplete if there was something specific to this file being loaded that you were tracking.

Link to comment
Share on other sites

You have lots of options actually. You can use SWFLoaders independently, creating one for each of your swfs and then just load() whichever one you want. Or you can put them all into a LoaderMax which is like a queue that manages loading of its children for you. The nice thing about that is you can set up the queue and let 'er rip. It'll load the children sequentially. Actually, by default it will load 2 at a time in order to maximize the use of the bandwidth and speed up the overall load of the whole queue. But you can set the maxConnections in that LoaderMax instance to 1 if you want it to load one at a time in sequence.

 

When you prioritize() a loader, it will automatically start loading it and temporarily shift it to the top of any LoaderMax instances to which it belongs. So if your LoaderMax has 4 loaders and the first one is currently loading and then you prioritize() the 4th one, it'll automatically stop loading the first one that's in progress and load the 4th one instead. Then when that one is done, it'll resume loading the rest of the LoaderMax children.

 

Make sense?

 

So yes, in your case it probably is best to put 'em into a LoaderMax queue to get them loading and then prioritize() whichever one you need at any given time. Hopefully by the time you need one, it'll already have preloaded and it'll be available almost instantly.

 

Thanks for the feedback guys,

Is there a way to show the loading progress / btyesLoaded of ONLY the currently prioritized loader? and not the entire que. Or if I want to do that should do I need to use separate loaders?

Link to comment
Share on other sites

Great thanks for the code guys.

Link to comment
Share on other sites

Is there a way to show the loading progress / btyesLoaded of ONLY the currently prioritized loader? and not the entire que. Or if I want to do that should do I need to use separate loaders?

Absolutely. You can get the progress/bytesLoaded/bytesTotal anytime for any loader.

 

myLoader.prioritize();
trace("current progress: " + myLoader.progress);

 

If you want to get only the currently loading children of a LoaderMax, you can use its getChildrenByStatus() method.

 

Does that answer your question?

Link to comment
Share on other sites

Is there a way to show the loading progress / btyesLoaded of ONLY the currently prioritized loader? and not the entire que. Or if I want to do that should do I need to use separate loaders?

Absolutely. You can get the progress/bytesLoaded/bytesTotal anytime for any loader.

 

myLoader.prioritize();
trace("current progress: " + myLoader.progress);

 

If you want to get only the currently loading children of a LoaderMax, you can use its getChildrenByStatus() method.

 

Does that answer your question?

 

Not sure that I understand Jack, and I cant seem to figure it out.

 

Let me try to explain where the hangups in my projects are:

 

Currently I have created a que that loads four swfs and everything works great. They load fine.

 

When a user clicks one of four buttons a corresponding .swf from the que is played. This also works perfectly.

 

When the que begins loading the four .swfs a progress bar animates and a text field displays information about the total mbs loaded and the time remaining.

 

I need three things to happen that I haven't yet figured out.

First, when a user clicks on one of the four buttons I want to prioritize the loading of the corresponding .swf from the que (which I think is working but I am not sure how to tell)

 

Second, I want the progress bar and progress information to change from displaying the overall que progress, to the progress of the individual loader/swf being prioritized by the clicked button.

 

Third, when the user clicks a button and prioritizes a .swf from the que, the .swf is also played. However, it will begin playing even if the .swf is not fully loaded and I don't want that. I only want the .swf to play if it has completed loading.

 

What I can not figure out how to do currently is:

A. Make the progress information correctly reflect the progress data of the prioritized loader and not the entire loader. Basically i cant figure out how to reference the individual loaders in the que. When I try to reference them by using the the "name" attribute I get an " Variable seq_1 is not defined." error. Which I assume must somehow be related to scope.

 

B. Check to see if individual loaders have completed loading so I can restrict playback to .swfs that have completed loading only.

 

Below is the relevant code I am using:

// Vars
var _queue = new LoaderMax({name:"sequenceLoader",onComplete:QueueComplete,onProgress:QueueProgress,onError:QueueError});

// LaoderMax Que
_queue.append( new SWFLoader("interactive_sequence.swf", {name:"seq_1", estimatedBytes:3000, container:this.heroMain_mc.sectionContainer_2_mc.container_mc, autoPlay:false}));
_queue.append( new SWFLoader("motion_sequence.swf", {name:"seq_2", estimatedBytes:3000, container:this.heroMain_mc.sectionContainer_3_mc.container_mc, autoPlay:false}));
_queue.append( new SWFLoader("coming_soon_sequence.swf", {name:"seq_3", estimatedBytes:3000, container:this.heroMain_mc.sectionContainer_4_mc.container_mc, autoPlay:false}));
_queue.append( new SWFLoader("coming_soon_sequence.swf", {name:"seq_4", estimatedBytes:3000, container:this.heroMain_mc.sectionContainer_5_mc.container_mc, autoPlay:false}));
_queue.load();


function QueueProgress(event:LoaderEvent):void
{
// show preloader
Object(this).loader_main_mc.visible = true;

var perc:Number = Math.abs(event.target.bytesLoaded / event.target.bytesTotal) * 100;
var secs:Number = ((1 / event.target.progress) - 1) * event.target.loadTime;
var frameNumber:Number = Math.round(event.target.progress * 100);

this.loader_main_mc.loaderAnimation_mc.gotoAndStop(frameNumber);
this.loader_main_mc.percent.text = String(perc.toFixed(2)) + " %" + " of " + (mbps.toFixed(2)) + " mb";
this.loader_main_mc.time.text = String(secs.toFixed(1)) + " seconds remaining";

//trace("progress: " + event.target.progress);
}

function QueueComplete(event:LoaderEvent):void
{
// hide preloader
Object(this).loader_main_mc.visible = false;

//trace('complete');
}

function QueueError(event:LoaderEvent):void
{
//trace("error occured with " + event.target + ": " + event.text);
}


// Code in button functions
var sec_1_Content = LoaderMax.getContent("seq_1").rawContent;
_queue.prioritize("seq_1");
sec_1_Content.gotoAndPlay(2);

 

 

As always any help from anyone is greatly appreciated. Thanks in advance.

Link to comment
Share on other sites

In your QueueProgress(event:LoaderEvent):void method, instead of:

var perc:Number = Math.abs(event.target.bytesLoaded / event.target.bytesTotal) * 100;

I think you can use the currentTarget property:

var perc:Number = Math.abs(event.currentTarget.bytesLoaded / event.currentTarget.bytesTotal) * 100;

But you will probably need some conditional logic code to work out the switch between overall progress and individual progress.

 

Edit: You can also use the currentTarget.progress property, which is "a value between 0 and 1 indicating the overall progress of the loader."

Link to comment
Share on other sites

In order to see if a loader is truly prioritized, you can view every loader's status and progress in your own little debug window.

 

add this line after your _queue is constructed

 

var childLoaders:Array = _queue.getChildren();

 

place a dynamic textfield called output on the stage and make sure it is set to handle multi-line text.

 

add the following to your QueueProgress()

 

 

	

output.text ="";
for(var n=0; n
		output.appendText( childLoaders[n].name+ "  : " + childLoaders[n].status + "  :  "  + childLoaders[n].progress + "\n" );

	}

 

this will loop through all your loaders and give you the name, status and progress of each loader.

 

this will allow you to see that a loader has been prioritized.

 

the status codes are as follows

0 LoaderStatus.READY

1 LoaderStatus.LOADING

2 LoaderStatus.COMPLETED

3 LoaderStatus.PAUSED

4 LoaderStatus.FAILED

5 LoaderStatus.DISPOSED.

 

To prevent your swf from playing, use autoPlay:false in the special properties vars for each SWFLoader

 

it seems that markavian gave you great info on having your loader bar display the info for an indvidual loader.

Link to comment
Share on other sites

Thanks for the feedback guys unfortunately this doesn't solve my dilema.

 

Carl, unfortunately autoPlay:false does not solve my problem. In fact I already have that in my code. The conflict here is that I have a button that explicitly tells the loaded .swf to play, and what I want to do is change the logic of that button so that it only tells the .swf to play IF it is completely loaded. So essentially the problem is that I don't know how to check the COMPLETE status of an appended item in the que.

 

Mark,

Unfortunately the currentTarget.progress still gives me the progress of the entire _queue and not individual loaders.

 

 

Thanks again for taking the time guys I do really appreciate it.

Link to comment
Share on other sites

from an older greensock post:

 

 var loader:SWFLoader = LoaderMax.getLoader("NameOrURLOfwhateverSwfYouAreTryingToPlay");
   if (loader.status == LoaderStatus.COMPLETED) {
       //do stuff immediately...
   } else {
trace("swf isn't loaded");

//in case you want to prioritize it now and then play it when it is done
//note you will have to create a completeHandler
       loader.addEventListener(LoaderEvent.COMPLETE, completeHandler);
       loader.prioritize();
   }

Link to comment
Share on other sites

Thanks Carl,

Ill try that. Also, that code you gave me to check if loaders were being prioritized helped a lot too. Turns out they were not. I had to change the prioritize snytax a bit (still not sure why but at this point I'll take it) and now they are prioritizing which looks like it might possibly have the trickle down effect of making the currentTarget.progress tip work as well.

 

Thanks again guys, great community. When I get some money coming in I will renew my greensock membership. I have donated before but it expired.

-A

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