As said in the previous post, Greensock also developped a loader, named LoaderMax, that makes loading external files to a flash application a piece of cake. Let’s see what we can make of it.
I’ll use the same flash demo I presented earlier, as it needs to load a series of images for a slide show as well as audio. The great feature of this loading system is that you can integrate subloaders to one main loader. There are, of course, many other awsome features like prioritizing, XML parsing, swf and video loading, but I don’t need them in this example. There’s no doubt I’ll be using them in the future though.
The first thing to do is declare an instance of LoaderMax intended as the main loader :
1 2 3 4 5 6 7 8 |
var mainLoader:LoaderMax = new LoaderMax({name:"mainLoader", maxConnections:1, onProgress:progressHandler, onComplete:completeHandler, onChildProgress:childProgressHandler, onChildComplete:childCompleteHandler, onError:errorHandler }); |
maxConnections
is the number of simultaneous connections you would like during the loading processonProgress
defines an event handler when overall loading is in progressonComplete
is an event handler fired when the overall loader has finished loadingonChildProgress
is like onProgress applied to each subloaderonChildComplete
is like onComplete applied to each subloaderonError
fires when loading fails
We’ll see how to deal with the event handlers later on.
For now, let’s concentrate on adding the subloaders :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
for (i=0;i<nbphoto;i++) { imageMC[i] = new MovieClip(); imageMC[i].name="img"+i; photoHolder.addChild(imageMC[i]); loadedImages[i] = new ImageLoader("img/img"+i+".jpg", {name:String("photo"+i), container:imageMC[i], estimatedBytes:100000, width:800, height:600 }); mainLoader.append(loadedImages[i]); } var musique = new MP3Loader("baby.mp3", {name:"audio", estimatedBytes:3000000, autoPlay:false, repeat:-1 }); mainLoader.append(musique); mainLoader.load(); |
As you can see, I loop through all my pictures and add an ImageLoader for each of them. I need a naming convention here, otherwise I’d probably have to go with an XML parser, but I’d have to update it if ever I added or changed the pictures. All I need here is to pass nbphotos
via an external variable.
A movie clip is created to be used as a container recieving the uploaded picture. You’ll notice you can define the picture size, there’s more cool stuff you can do by just passing over parameters to the imageLoader, cropping and scaling come to mind. estimatedBytes
allows you to give the loader an idea of the image weight before it actually gets the information, this is intended for loading progress accuracy.
The MP3Loader is pretty much the same, with specific features. autoPlay
let’s the music start playing as soon as the loader has buffered enough data, whereas repeat
is the number of the times it should repeat, -1 being forever. The great thing is you can then control the audio with the mp3Loader’s instance’s methods and properties, like musique.pauseSound()
, musique.playSound()
, or musique.volume=0.5
Once the subloaders are all added to the main loader, all you need to do is start the loading process.
Well, this is all well, but the interesting bit is to have the progress display on your screen. This is where the event handlers come in.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function progressHandler(event:LoaderEvent):void { preloader.progressbar.scaleX=event.target.progress; preloader.percent.text=String(Math.floor(event.target.progress*100))+" %"; } function childProgressHandler(event:LoaderEvent):void { if (currentLoad<=nbphotos) { txt = "Loading picture "+String(currentLoad)+"/"+String(nbphotos); } else { txt = "Loading audio"; } preloader.childprogressbar.scaleX=event.target.progress; preloader.childelement.text=txt; } function childCompleteHandler(event:LoaderEvent):void { currentLoad++; } function completeHandler(event:LoaderEvent):void { //do stuff } |
Without going into all the details, you can see how a progrees bar is easily scaled according to the loading progress. This works the same for the main loader and the subloaders. I use a counter which increments each time a subloader completes, so I know which image is currently loading, or if it’s the audio. I guess this isn’t very accurate, I should get this info directly from the event handler. What happens if one of the loader fails ? it beats me, I should test that sometime…
Anyway, here’s a screenshot of what it looks like, and here’s a link to the demo, so you can actually see it work 🙂
Just to finish off, I’d like to suggest this tutorial from Carl Schooff, one of GreenSock’s staff members, that really helped understand the basics of LoaderMax, not to mention he’s also been great help on the GreenSock forum, solving some of my issues.
This actually answered my drawback, thank you!
Not at all, I’m glad I could help !