View Full Version : Need Preloader for multiple swf
thevibe
December 12th, 2009, 03:58 AM
Hi guys,
I posted a tread about this yesterday but I think I wasn't clear enough so here's the details of my project. Lets say I have 5 page to load and I want them to load all at the same time. The address (not real) of the pages could be;
http://www.adddress.com/aboutus/
http://www.adddress.com/contact/
http://www.adddress.com/services/
http://www.adddress.com/products/
http://www.adddress.com/list/
In each folder, there's a index.html that have a swf called main.swf in it.
So for example, if someone writes http://www.adddress.com/contact/ I want a preloader to start and load the 5 main.swf in the 5 folders and when it's done, it should play the main.swf of the address written (in this example contact).
So in this example I imagine I would have a preloader.swf in every folder, since I want to be sure that the user will preload the entire website whatever the way he uses to enter it (from index-from sitemap-from direct address or bookmark).
And my last concern is that I want to be sure that once the loading (for all the pages) have been executed, when the user enters the other pages they won't see the preloader or it will take less than 1 sec to go trough it.
If you have any questions please ask them, I really need to find a solution for that. And I am new to AS3 so please if you have the answer, put as much details as you can.
Thanks in advance !!!!
dustinsmith08
December 12th, 2009, 03:22 PM
I think i understand what your looking for so let me take a stab at it. Basically you want a way to load all the pieces of your website separately. One of the best ways I know to do this is the create as index.swf which is only responsible for loading and holding all the other swfs you want to load. All this index.swf has in it is a little actionscript and a loadbar. This means it will load fast and show users even on a slow connection that the page is loading. I made up a loader class for you, (which i will probably use in some of my projects later). There are four files, the loader (index.fla) and three sample fla files that it loads. All the action script is in the first frame. In the beginning you should see where I tell the loader to load each file. You can of course change these urls, add more, etc. The load bar will show to total progress of all the files you tell the loader to import. Once all the files are loaded it will add them to the stage in the order you told the loader to load them. It will then attempt to trigger a function in each loaded swf and send it an array with all the other swfs, so each swfs can talk with the other swfs in the site.
Bellow is the code I put in the index.fla - or you can just download the zip file
var totalBytes:Number = 0
var loadingArray:Array = new Array()
var tobeLoaded:int = 0 //counts how many object need to be loaded before the pages is ready
// once all the swfs are loaded we will need to add them in the right order so they are layered right
//list of swfs to be loaded
loadSWF("load1.swf")
loadSWF("load2.swf")
loadSWF("load3.swf")
function loadSWF(url:String){
var myLoader:Loader = new Loader
myLoader.contentLoaderInfo.addEventListener(Progre ssEvent.PROGRESS, updateLoaded)
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, loadingDone)
myLoader.contentLoaderInfo.addEventListener(IOErro rEvent.IO_ERROR, ioErrorHandler);
var loadObject:Object = new Object() // creates an object that is unqite to this loading proccess and can be called by name as long as you do it inside this function
// an object can be a lot of things - arrays, movieclips, etc. - in this case we are using it as a variable that can have information attached to it which is dynamically updated
loadObject.byteLoaded = 0 // attach variable to the object
loadingArray.push(loadObject) // adds the load object to an array so that we can access the the object outside of the load function
var bytesAddedToTotal:Boolean = false //this is used bellow to add the bytes total to 'totalBytes' only once
tobeLoaded ++
myLoader.load(new URLRequest(url))
//
function updateLoaded(e:ProgressEvent){ //since this function is inside the loadSWF function, each load proccess will be given its own 'updateLoaded' function to use
if(!bytesAddedToTotal){// the '!' means if this is false
totalBytes += e.bytesTotal
bytesAddedToTotal = true // change var to true so that this function is not run again
}
loadObject.byteLoaded = e.bytesLoaded // update the object attached to this load proccess
}
//
function loadingDone(e:Event){ // since this function is outside the loadSWF function it is what all the load proccesses call
trace("loading done")
tobeLoaded --
loadObject.swf = e.target.content
LoadingDone()
}
function ioErrorHandler(){//if there is and error - most likly that the url is incorrect
tobeLoaded --
loadingArray.splice(loadingArray.indexOf(loadObjec t),1) //removes the object from array
trace("unable to load: "+url)
}
}
addEventListener(Event.ENTER_FRAME, updateLoadBar)
var total:Number = 0
function updateLoadBar(e:Event){
if(loadingArray.length > 0){
total = loadingArray[0].byteLoaded
for(var i:int=1; i<loadingArray.length; ++i) total += loadingArray[i].byteLoaded
total = Math.round((total/totalBytes)*100)
progressBar.progress_text.text = total + "%"
}
}
function LoadingDone(){
if(tobeLoaded == 0){
removeEventListener(Event.ENTER_FRAME, updateLoadBar) //stops progress bar
//progressBar.visible = false
//
for (var p:int=0; p<loadingArray.length; ++p){
addChild(loadingArray[p].swf)
//bellow us a line of code that will call 'startSWFfunction' in the loaded swf
//you can change startSWFfunction to whatever name you want
// this does have an erro catch, but it is best if you have this function
//on the first frame of all the swfs you load, with the same amount of variables
// passed into each
//i passed in the loading array because the makes it easy for each of the swfs to call
//function in the other swfs - just use loadingArray[
try{loadingArray[p].swf.startSWFfunction(loadingArray)}catch(e:Error) {trace("failed to trigure function")} // un comment this if you want to trigure a function once the swf is loaded
//
}
}
}//
thevibe
December 13th, 2009, 02:24 AM
Thanks for your reply, but sadly this is not what I am looking for. I was really looking forward to check your code since you put so much details and even an example!!! What I need is a preloader not a loader, that will cache all the swf but not load them to the stage. The way the website was design, they are separate entities that can be reach by their own addresses. So for example, you can write www.test.com/contact and directly arrive to the contact page. From there, a preloader should start and preload (cache) all the pages, after that it could go trough the called page (e.g.contact). I would really like it if you could check it out again and see if you can help me. And feel free to ask any questions if needed to clarify my issue. THANKS!!!
GarFonz
December 13th, 2009, 09:34 PM
I would use a SharedObject object to store information about the success/failure of the other .swfs, as well as the .swf files themselves. the preloader would check for sharedObject.getLocal("main1.swf"); while main0.swf is loading/playing. You could use an Event.COMPLETE to reassign a boolean value to the shared object once one of the main.swf files is completely downloaded. The sharedObject should persist between downloads from the server.
dustinsmith08
December 17th, 2009, 04:25 PM
preloading is easy. just load the pages like normal into a loader swf (like my example), but don't add them to the stage until you want them to be displayed. Just have your buttons on the currently displayed page tell a function to remove the current page and add the page the button is linked to. You could even add a check in the function, and display the load progress instead if the page is still being loaded.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.