well, sometime you have several files that must be loaded in mainMovie, earlier or later. it is better earlier
suppose that you have mainMovie with some subMovies to load, and when mainMovie starts it waits for user action and then loads subMovie in accordance with user action.
would'nt it be nice to preload subMovies in background while user reads main page?
is it possbile ?
YES !
let's see how it can be done
CODE
onload() { // by cybernetix 13dots.com
// let's create some nice object which will hold everything
// MUST BE A GLOBAL VAR
_global.ploader=new Object();
// quey for movies to load
ploader.quey=new Array();
// quey for loaded movies
ploader.loadedMovies=new Array();
// ok, we have to load files somewhere, so xml will be fine :)
// yes, IT IS POSSIBLE TO LOAD ANYTHING INTO XML OBJECT, BUT
// IT DOES NOT MEAN that you can read it using xml methods.
// in this case we shall use it just to load files in cache
// for later quick use.
ploader.loader=new XML();
// function which will load files, one by one
ploader.loadNext=function() {
// let's see is there any file in a quey
if ( this.quey.length > 0 ){
// just to be sure that file exists, ANOTHER XML TRICK
this.loader.onload=function(success) {
// if file is loading parameter success will be TRUE
// otherwise will be FALSE, so we are intersting in
// only if it fails
if (!success) {
// file not found, call function to let us know
// what is going on
ploader.ploaderHalted();
}
};
// load file
this.loader.load(this.movieName=this.quey.shift());
// call function dataControl with interval, just to control
// what happens with loading. interval=100, you can change
// it to whatever, but if you have small files it is better
// to be short interval.
this.intervaller = setInterval(this, "dataControl", 100);
} else {
// nice, all files loaded
// remove this javascript line in your movie and place your action
// eg. gotoSceneAndPlay("Scene_2",1);
javascript("alert('ALL MOVIES PRELOADED')");
}
};
// function which will tell us what is going on
ploader.dataControl=function() {
// let's see percetnage of loaded
this.percent=this.loader.percentLoaded();
// here you can display which file loads and percent of load
/*
someDynamicTextField_1.text=this.movieName;
someDynamicTextField_2.text=this.percent;
*/
// if loaded completely
if (this.percent == 100) {
// clear interval
clearInterval(this.intervaller);
// put this file in loaded area
this.loadedMovies.push(this.movieName);
// load next file
this.loadNext();
}
};
// function that will be invoked within loader.onload function
// and will be used if some file can not be loaded
ploader.ploaderHalted=function() {
// can not load file, take some action here
// remove this javascript line in your movie and place your action
javascript( "alert('CAN NOT LOAD MOVIE: '+'"+ploader.movieName+"')" );
};
// this one will tell us if file is loaded
// use it from anywhere like this
/*
if (ploader.isLoaded("file.swf or jpg or whatever")) {
fine, it is loaded, do this
}
else {
it is not loaded, do that
}
*/
// NOTE: it will return false if you pass to function file name
// which is not listed in ploader.quey
ploader.isLoaded=function(movieName) {
// initiate control var
ploader.movieLoaded=false;
// let's have a look in loadedMovies
for (i=0;i<ploader.loadedMovies.length;i++) {
// if it is there, it is loaded
if (ploader.loadedMovies[i]==movieName) {
ploader.movieLoaded=true;
}
}
// if not found in ploader.loadedMovies
// var ploader.movieLoaded will remain false
// now, return true or false
return ploader.movieLoaded;
};
// here you should place files to preload in order as you wish
ploader.quey.push("movie1.swf");
ploader.quey.push("file1.txt");
ploader.quey.push("image1.jpg");
// here we go, this one will start preloading
ploader.loadNext();
// NOTE - somewhere later you can destroy ploader object, to release memory
// but ONLY if you decide to wait for all movies to preload, otherwise you
// will need it later to know which one is loaded.
/*
ploader=undefined;
*/
}
// here is the point where you can decide if you want movie to wait for all
// movies to be preloaded first or you can remove stop(); so main movie will
// play while preloading other subMovies in background.
onFrame(1) {
stop();
}
// with this preloader you can preload ALL KIND OF FILES.
//
just put this code in the _root, change file names in ploader.quey,
you can add some more of course and test it.