Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preloader for al swf-s that willl be used

Status
Not open for further replies.

brutteforcce

Technical User
Jun 23, 2006
105
RO
I use a lot in my site a movie clip loader called myMCL. Every time it loads something the preloader is working. Firt I thought that it is a good idea but now I want that preloader to preload all the swf-s that I load using the movie clip loader...

stop();
//--------------------<MCL>-----------------------\\
var myMCL:MovieClipLoader = new MovieClipLoader();
var myListener:Object = new Object();

myMCL.addListener(myListener);

myListener.onLoadProgress = function(target_mc:MovieClip, loadedBytes:Number, totalBytes:Number) {
_level50._visible = true;
var preloadPercent:Number = Math.round((loadedBytes / totalBytes) * 100);
_level50.preloader.gotoAndStop(preloadPercent);
}

myListener.onLoadComplete = function(target_mc:MovieClip) {
_level50._visible = false;
}
//--------------------</MCL>-----------------------\\

// trigger the MCL to load these assets:
myMCL.loadClip("trigger.swf", 5);
myMCL.loadClip("preloader.swf", 50);

Please help... Thanks!
 
Thanks but the example from that tutorial works like mine... When one of the swf is loaded it apears and I have a lot of loading from the loaded swf-s...
So I want all the swf-s to be preloaded at the begining...
From the master file main_menu.swf is loaded. From the main menu I'm loading five swf-s when the user is pressing the corresponding button. The second button opens another menu and from that menu other swf-s can be loaded...
So I want all the swf-s to be loaded at the begin and after the site will work fast...
Please help...
 
The strategy would be to create an array contains the list of SWF, then fire MovieClipLoader.loadClip method for the first SWF in the array. When onLoadComplete is invoked, move onto the second SWF and fire loadClip for the second SWF. Then 3rd, 4th, etc, etc.

Kenneth Kawamoto
 
Thanks a lot.
How can I make a preloader that shoes the progress and the percent for all the swf-s... Now, the preloader reaches 100 for one swf and starts again for another, until it finishes preloading the array.
 
Yes, this is the fun bit.

Say you have 100 SWFs to load. The first SWF will consume 1/100 of the loader bar. The second will start from 1/100 and end at 2/100, and so on. All 100 SWFs have different byte sizes so this technique will not represent the true downloading progress, but it's only a preloading at the end of the day...

Kenneth Kawamoto
 
If you ignore the total bytes count of all loaded movies, then the only way is to first initiate a loading of all files in the array, interrupting each loading as soon (a few milliseconds...) as the total bytes count of that particular movie is known, so that you can you can add those bytes counts and store the result in a global bytes count variable.
You then re-initiate the actual loading of all files in the array, and base the total percentage and/or the loadbar's progress or width, on the previously calculated global bytes count of all movies...

I have such a custon preloader... But I don't think this would work with the movieClipLoader.

Regards. Web Hosting - Web Design
03/13/05 -> OLDNEWBIE VS FLASHKIT
 
I think that's also possible with MovieClipLoader in theory, oldnewbie. As soon as onLoadProgress is fired (therefore bytesTotal is known), just unloadClip() to kill the download and move onto the next brief SWF loading. Then once you collected all the bytesTotal of the SWFs start loading them properly sequentially.

Overkill, may be, but why not!

Kenneth Kawamoto
 
Thanks a lot... You have good ideas but how can I do that? Please help me! Thanks again!
 
If I find the total bytes for the swf-s I can use it directly without having to find it every time? How can I simulate the download of that size in the same time with the loading of all the swf-s. Please help me!!! Thanks!
 
Of how many external movies are we talking about here?

Unless you're talking in the hundreds, and of most users all being on some old slow connections, the extra step of cumulating the exact total bytes of all movies, is meaningless in terms of time. A few seconds at most... And once cached, even less...

In a custom preloader, or as Kenneth is suggesting it would be the same with the movieClipLoader (I've never tried it!), you would simply replace the total bytes bit of each individual file, with the calculated actual global bytes count of all movies, within your info displaying scripts, and/or loading bar's width.

Regards. Web Hosting - Web Design
03/13/05 -> OLDNEWBIE VS FLASHKIT
 
If I load and unload a movie can I load it again in another level without having to download it again? I found the total bytes of all the swf-s. How can I make the preloading progress to work correctly? Help please!
Thanks...
 
It isn't calculating the total bytes correctly.

myListener.onLoadProgress = function(target_mc:MovieClip, loadedBytes:Number, totalBytes:Number) {
if (target_mc == _level75) {
trace("totalBytes = "+totalBytes);
bytesNum = bytesNum + totalBytes;
trace("bytesNum = "+bytesNum);
}

}

for (var i:Number = 0; i < swfNum; i++) {
myMCL.loadClip(swfList,75);
trace(swfList);
}

After I simulate the download I get in the output:

trigger.swf
preloader.swf
main_menu.swf
initial.swf
static.swf
totalBytes = 1047
bytesNum = 1047
totalBytes = 1047
bytesNum = 2094
totalBytes = 1047
bytesNum = 3141
totalBytes = 1047
bytesNum = 4188
totalBytes = 1047
bytesNum = 5235

It just adds the size of the last one... If I replace i with 0, 1, 2 ,3, 4 it shoes the totalBytes correctly.
What should I do?

Thanks.
 
You can't use "for" loop. Here's my version:
Code:
// AS2 main timeline
var swfList:Array = ["1.swf", "2.swf", "3.swf", "4.swf", "5.swf", "6.swf"];
var loadID:Number = 0;
var grossBytes:Number = 0;
var myMCL:MovieClipLoader = new MovieClipLoader();
var myListener:Object = new Object();
myMCL.addListener(myListener);
myListener.onLoadProgress = function(target_mc:MovieClip, loadedBytes:Number, totalBytes:Number):Void  {
	if (totalBytes) {
		trace("SWF: "+target_mc._url);
		trace("totalBytes: "+totalBytes);
		grossBytes += totalBytes;
		trace("grossBytes so far: "+grossBytes);
		trace("***");
		loadID++;
		if (loadID<swfList.length) {
			testLoad(loadID);
		} else {
			trace("Final grossBytes: "+grossBytes);
			myMCL.unloadClip(mcTemp);
			mcTemp.removeMovieClip();
		}
	}
};
function testLoad(loadID:Number):Void {
	mcTemp.removeMovieClip();
	this.createEmptyMovieClip("mcTemp", 1);
	myMCL.loadClip(swfList[loadID], mcTemp);
}
testLoad(loadID);
stop();

//

I get the Output:

SWF: file:///D|/flash/temp/1.swf
totalBytes: 313701
grossBytes so far: 313701
***
SWF: file:///D|/flash/temp/2.swf
totalBytes: 64325
grossBytes so far: 378026
***
SWF: file:///D|/flash/temp/3.swf
totalBytes: 15206
grossBytes so far: 393232
***
SWF: file:///D|/flash/temp/4.swf
totalBytes: 233
grossBytes so far: 393465
***
SWF: file:///D|/flash/temp/5.swf
totalBytes: 518
grossBytes so far: 393983
***
SWF: file:///D|/flash/temp/6.swf
totalBytes: 141170
grossBytes so far: 535153
***
Final grossBytes: 535153

Kenneth Kawamoto
 
Thanks a lot!!! That did the job.
After I find the grossBytes I have tot preload the movies and then load them in the correct place... I tryed to make that but it didn't work correctly. When I tryed to simulate the download a program called Crash Doctor gived me the option to terminate Flash. I supose something is wrong. What should I do?
Please help!!!
Thanks...
 
I still didn't managed to make the preloader work correctly.
I don't know how to calculate the preload percent...
This is the code:

Code:
stop();
//--------------------<MCL>-----------------------\\
var swfList:Array = ["trigger.swf", "preloader.swf", "initial.swf", "static.swf", "main_menu.swf"];
var loadID:Number = 0;
var grossBytes:Number = 0;
var myMCL:MovieClipLoader = new MovieClipLoader();
var myListener:Object = new Object();
myMCL.addListener(myListener);
function loading() {
	myListener.onLoadProgress = function(target_mc:MovieClip, loadedBytes:Number, totalBytes:Number) {
		_level50._visible = true;
		var preloadPercent:Number = Math.round((loadedBytes/grossBytes)*100);
		_level50.preloader.gotoAndStop(preloadPercent);
		trace(preloadPercent);
		trace(grossBytes);
	};
	myListener.onLoadComplete = function(target_mc:MovieClip) {
		_level50._visible = false;
	};
	myMCL.loadClip("trigger.swf", 5);
	myMCL.loadClip("preloader.swf", 50);
}
myListener.onLoadProgress = function(target_mc:MovieClip, loadedBytes:Number, totalBytes:Number):Void  {
	if (totalBytes) {
		trace("SWF: "+target_mc._url);
		trace("totalBytes: "+totalBytes);
		grossBytes += totalBytes;
		trace("grossBytes so far: "+grossBytes);
		trace("prima data = "+firstTime);
		trace("***");
		loadID++;
		if (loadID<swfList.length) {
			testLoad(loadID);
		} else {
			trace("Final grossBytes: "+grossBytes);
			myMCL.unloadClip(mcTemp);
			mcTemp.removeMovieClip();
			firstTime = false;
			trace("prima data = "+firstTime);
			loading();
		}
	}
};
function testLoad(loadID:Number):Void {
	mcTemp.removeMovieClip();
	this.createEmptyMovieClip("mcTemp", 1);
	myMCL.loadClip(swfList[loadID], mcTemp);
}
testLoad(loadID);
stop();
//
//--------------------</MCL>-----------------------\\

What should I modify so it would work?

Please help me!!!
Any help is appreciated...
Thanks...
 
Once you determine the aggregated total file size, you should start loading SWFs one by one so that you can monitor the progress; i.e. start downloading the second SWF in the list after the first one is downloaded fully.

Kenneth Kawamoto
 
Thanks, but I got the idea from the begining... I just can't make it work... Can you help me please?
Thanks...
 
OK, this is getting a bit complicated...
Code:
// AS2 main timeline
var swfList:Array = ["1.swf", "2.swf", "3.swf", "4.swf", "5.swf"];
var swfBytesList:Array = new Array();
var loadID:Number = 0;
var isTestLoad:Boolean = true;
var grossBytes:Number = 0;
var bytesCompleted:Number = 0;
var myMCL:MovieClipLoader = new MovieClipLoader();
var myListener:Object = new Object();
myMCL.addListener(myListener);
myListener.onLoadProgress = function(target_mc:MovieClip, loadedBytes:Number, totalBytes:Number):Void  {
	if (isTestLoad) {
		if (totalBytes) {
			swfBytesList.push(totalBytes);
			trace("SWF: "+target_mc._url);
			trace("totalBytes: "+totalBytes);
			grossBytes += totalBytes;
			trace("grossBytes so far: "+grossBytes);
			trace("***");
			loadID++;
			if (loadID<swfList.length) {
				testLoad(loadID);
			} else {
				trace("Final grossBytes: "+grossBytes);
				trace("***");
				myMCL.unloadClip(mcTemp);
				mcTemp.removeMovieClip();
				trace("Start sequential loading");
				trace("***");
				isTestLoad = false;
				loadID = 0;
				sequentialLoad(loadID);
			}
		}
	} else {
		trace("Currently loading "+target_mc._url);
		var bytesSoFar:Number = bytesCompleted+loadedBytes;
		trace("Bytes loaded so far: "+bytesSoFar);
		var percentageLoaded:Number = Math.floor(10000*bytesSoFar/grossBytes)/100;
		trace(percentageLoaded+"%");
		trace("***");
	}
};
myListener.onLoadComplete = function(targetMC:MovieClip, httpStatus:Number):Void  {
	bytesCompleted += swfBytesList[loadID];
	trace("Load completed for "+targetMC._url);
	trace("***");
	loadID++;
	if (loadID>=swfList.length) {
		trace("All loadings completed!");
	} else {
		sequentialLoad(loadID);
	}
};
function sequentialLoad(loadID:Number):Void {
	var ID:Number = loadID+1;
	var mc:MovieClip = this.createEmptyMovieClip("mc"+ID, ID);
	myMCL.loadClip(swfList[loadID], mc);
}
function testLoad(loadID:Number):Void {
	mcTemp.removeMovieClip();
	this.createEmptyMovieClip("mcTemp", 1);
	myMCL.loadClip(swfList[loadID], mcTemp);
}
testLoad(loadID);
stop();
//

What I get in the Output is as follows:
[tt]
SWF: file:///D|/flash/sequential%5Fload/1.swf
totalBytes: 313701
grossBytes so far: 313701
***
SWF: file:///D|/flash/sequential%5Fload/2.swf
totalBytes: 15496
grossBytes so far: 329197
***
SWF: file:///D|/flash/sequential%5Fload/3.swf
totalBytes: 198600
grossBytes so far: 527797
***
SWF: file:///D|/flash/sequential%5Fload/4.swf
totalBytes: 233
grossBytes so far: 528030
***
SWF: file:///D|/flash/sequential%5Fload/5.swf
totalBytes: 518
grossBytes so far: 528548
***
Final grossBytes: 528548
***
Start sequential loading
***
Currently loading file:///D|/flash/sequential%5Fload/1.swf
Bytes loaded so far: 65732
12.43%
***
Currently loading file:///D|/flash/sequential%5Fload/1.swf
Bytes loaded so far: 131314
24.84%
***
Currently loading file:///D|/flash/sequential%5Fload/1.swf
Bytes loaded so far: 196971
37.26%
***
Currently loading file:///D|/flash/sequential%5Fload/1.swf
Bytes loaded so far: 262645
49.69%
***
Currently loading file:///D|/flash/sequential%5Fload/1.swf
Bytes loaded so far: 313701
59.35%
***
Load completed for file:///D|/flash/sequential%5Fload/1.swf
***
Currently loading file:///D|/flash/sequential%5Fload/2.swf
Bytes loaded so far: 329197
62.28%
***
Load completed for file:///D|/flash/sequential%5Fload/2.swf
***
Currently loading file:///D|/flash/sequential%5Fload/3.swf
Bytes loaded so far: 527797
99.85%
***
Load completed for file:///D|/flash/sequential%5Fload/3.swf
***
Currently loading file:///D|/flash/sequential%5Fload/4.swf
Bytes loaded so far: 528030
99.9%
***
Load completed for file:///D|/flash/sequential%5Fload/4.swf
***
Currently loading file:///D|/flash/sequential%5Fload/5.swf
Bytes loaded so far: 528548
100%
***
Load completed for file:///D|/flash/sequential%5Fload/5.swf
***
All loadings completed!
[/tt]

You can see SWFs are loaded sequentially and the loaded percentage is accumulated to 100%.

I would not say this code is complete, as I didn't put any error handling, but I hope you'll get the basic idea.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top