MovieClip Loader Class AS2
check this out!
But now MovieClipLoader class lets you implement listener callbacks that provide status information while SWF or JPEG files are being loaded (downloaded) into movie clips.
After you issue the MovieClipLoader.loadClip() command, the following events take place in the order listed:
When the first bytes of the downloaded file have been written to disk, the MovieClipLoader.onLoadStart() listener is invoked. If you have implemented the MovieClipLoader.onLoadProgress() listener, it is invoked during the loading process. Note: You can call MovieClipLoader.getProgress() at any time during the load process.
When the entire downloaded file has been written to disk, the MovieClipLoader.onLoadComplete() listener is invoked. After the downloaded file's first frame actions have been executed, the MovieClipLoader.onLoadInit() listener is invoked. After MovieClipLoader.onLoadInit()has been invoked, you can set properties, use methods, and otherwise interact with the loaded movie.
If the file fails to load completely, the MovieClipLoader.onLoadError() listener is invoked.
Heres a little example of how it goes:
var wwinc_mcLoader = new MovieClipLoader();
wwinc_Listener = new Object();
/*onLoadStart*/
wwinc_Listener.onLoadStart = function(target_mc) {
/*your statements go here*/
};
/*on initialize (frame 1 of target movie clip*/
wwinc_Listener.onLoadInit = function(target_mc) {
/*your statements go here*/
};
/*loading done..*/
wwinc_Listener.onLoadComplete = function(target_mc) {
/*your statements go here*/
};
wwinc_Listener.onLoadError = function(target_mc, errorCode) {
trace("ERROR CODE = " + errorCode);
/*take an action here...*/
};
/*adding the listener*/
wwinc_mcLoader.addListener(wwinc_Listener);
/*finally load the image*/
wwinc_mcLoader.loadClip("http://webwizardinc.com/img/someimage.jpg", some movieclip
SIMPLER SYNTAX
var oImgListener = new Object();
oImgListener.onLoadInit = function(target_mc)
{
trace(target_mc._name + " load complete");
}
var mclImg = new MovieClipLoader();
mclImg.addListener(oImgListener);
mclImg.loadClip("http://myurl.com/path/to/swf/or/jpg", mcImgLoader);
A bit of theory
The MovieClipLoader class gives us full control on the loading of SWF and pictures, from the start to the initialization of the loaded asset.
The MovieClipLoader class is implemented, as we will see, by registering a listener object that will receive notifications when a MovieClipLoader event handler is invoked.
The MovieClipLoader class is composed of 5 event methods that are used to monitor the loading of an asset from start to finish. These 5 event methods are onLoadStart, onLoadProgress, onLoadComplete, onLoadInit and onLoadError. The beauty of these events is that they are giving us precious information at some key points of the loading and we can use that information to create an advanced preloading system (see my tutorial on creating a preloader). Let's have a look at the picture below :
1. onLoadstart([target_mc:MovieClip]) is invoked as soon as the first byte of data of the asset being loaded has been written on the hard drive. The parameter target_mc returns the name of the container receiving the loaded asset, that parameter is optional.
2. onLoadProgress([target_mc:MovieClip], loadedBytes:Number, totalBytes:Number) is invoked every time the loading content is written to the hard drive during the loading process. loadedBytes returns the number of bytes loaded since the loading started and totalBytes returns the total size of the asset being loaded, again the first parameter is optional.
3. onLoadComplete([target_mc:MovieClip], [httpStatus:Number]) is invoked as soon as the last byte of data of the asset being loaded has been written on the hard drive. Both parameters are optional.
4. onLoadInit ([target_mc:MovieClip]) is invoked as soon as the loaded asset has been initialized and is ready for action.
5. onLoadError(target_mc:MovieClip, errorCode:String, [httpStatus:Number]) is invoked if the server is down, the file is not found, or a security violation occurs.
More methods
Along with these 5 event methods, the MovieClipLoader class makes use of 5 methods that need to be mentioned :
- loadClip(url:String, target:Object) We use this method to load the SWf or picture
- getProgress(target:Object) Returns the number of bytes loaded and the total number of bytes of a file that is being loaded
- addListener(listener:Object) Registers an object to receive notification when a MovieClipLoader event handler is invoked.
- removeListener(listener:Object) Removes the listener that was used to receive notification when a MovieClipLoader event handler was invoked.
- unloadClip(target:Object) Removes a movie clip that was loaded by using MovieClipLoader.loadClip().
MORE CODE ::
_root.createEmptyMovieClip("container",1);
myLoaderListener = new Object();
myLoaderListener.onLoadStart = function(myContainer){
trace(myContainer+" started receiving data");
}
myLoaderListener.onLoadProgress = function(myContainer,loadedBytes,totalBytes){
trace(myContainer+" has loaded "+loadedBytes+" of "+totalBytes);
}
myLoaderListener.onLoadComplete = function(myContainer){
trace(myContainer+" has loaded the asset");
}
myLoaderListener.onLoadError = function(myContainer,errorCode, httpStatus){
trace("problem loading the data, the error code is "+errorCode+" and the httpStatus is "+httpStatus);
}
myLoaderListener.onLoadInit = function(myContainer){
trace("The data is loaded and ready for action so let's remove the listener to free up some memory ");
myLoader.removeListener(myLoaderListener);
}
myLoader = new MovieClipLoader();
myLoader.addListener(myLoaderListener);
myLoader.loadClip("bird.jpg","container");
function showProgress(){
trace(myLoader.getProgress(container).bytesLoaded);
}
No comments:
Post a Comment