Gone"loadMovie is gone J I thought it was pretty useless.
With Actionscript 3.0 and Flash CS3, all those who never had the chance to learn about the MovieClipLoader class of Flash 8 will have that chance now, once and for all!
AS 3.0 implements the Loader class to allow the loading of files out of the main swf.
In the following example I"ll explain how to use it at its best and you"ll see how simple it is.
As always, I create an FLA and save it as test_load.fla
I create a MovieClip and I call it info_mc, inside of which I create the necessary graphics to give it the look of a "Loader", hence with a text field which will show the percentage of loading and a progress bar to do the same.
I create an AS file and I call it Loading.as saving it in the same folder where the FLA is.
Loading.as ( by now it"s clear ) is our Document Class, written this way:
Code:
package {
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.text.TextField;
import flash.display.Loader;
import flash.events.*;
import flash.net.URLRequest;
public class Loading extends MovieClip
{
private var url:String;
private var loader:Loader;
public function Loading()
{
stage.frameRate=31;
info_mc.visible=false;
init();
}
private function init():void
{
url='http://www.flepstudio.org/swf/my_swf.swf"cachebuster='
+new Date().getTime();
var request:URLRequest=new URLRequest(url);
loader=new Loader();
initListeners(loader.contentLoaderInfo);
loader.load(request);
}
private function initListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE,completato);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR,seErrore);
dispatcher.addEventListener(Event.OPEN,inizia);
dispatcher.addEventListener(ProgressEvent.PROGRESS,inCaricamento);
}
private function initButtonListener():void
{
ricarica_btn.addEventListener(MouseEvent.CLICK,scarica);
}
private function inizia(event:Event):void
{
removeButtonListener();
info_mc.visible=true;
debug_txt.text='INIZIO';
}
private function inCaricamento(event:ProgressEvent):void
{
var n:uint=(event.bytesLoaded/event.bytesTotal)*100;
info_mc.loading_txt.text='Loading '+n.toString()+' %';
var nn:uint=(event.bytesLoaded/event.bytesTotal)*info_mc.border_mc.width;
info_mc.fill_mc.width=nn;
debug_txt.appendText('.');
}
private function completato(event:Event):void
{
debug_txt.appendText('FINITO');
info_mc.visible=false;
addChild(loader);
loader.x=10;
loader.y=10;
loader.width=loader.height=200;
initButtonListener();
}
private function seErrore(event:IOErrorEvent):void
{
trace("ioErrorHandler: "+event);
}
private function scarica(event:MouseEvent):void
{
removeChild(loader);
removeListeners(loader.contentLoaderInfo);
init();
}
private function removeButtonListener():void
{
ricarica_btn.removeEventListener(MouseEvent.CLICK,scarica);
}
private function removeListeners(dispatcher:IEventDispatcher):void
{
dispatcher.removeEventListener(Event.COMPLETE,completato);
dispatcher.removeEventListener(IOErrorEvent.IO_ERROR,seErrore);
dispatcher.removeEventListener(Event.OPEN,inizia);
dispatcher.removeEventListener(ProgressEvent.PROGRESS,inCaricamento);
}
}
}
Result:
Obviously I"ve created the swf to load where I"ve inserted a 1.7MB image to be able to notice all loading stages.
Source files:
Bookmarks