Some of you would say"finally!
FlepStudio has thought about its kind users, beginners with Flash CS3.
After having noticed the difficulties of certain users to the first approach with Flash CS3, after having read some comments of the articles that explain
how to load an external SWF and of the
tutorial Preloader, FlepStudio has realized a preloader for beginners. I do not think that you can find easier then that one.
This article will show you how to simply load an external SWF and, as always, the source files are included. No use of Class or file .as!
Everything is placed on the Timeline. Follow me and I will show you how"
I create a FLA and save it as "main.fla", inside which I place on stage a MovieClip with an instance name "preloader_mc".
First of all, let"s think about the fact that this preloader will be visible during the loading of the external SWF and then, once the loading finished, it will have to disappear.
I open the action panel and write:
Code:
preloader_mc.stop();
preloader_mc.visible=false;
var swf:String='http://www.flepstudio.org/swf/principianti/preloader/test.swf';
var richiesta:URLRequest=new URLRequest(swf);
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.OPEN,inizia);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,inCaricamento);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completato);
loader.load(richiesta);
function inizia(e:Event):void
{
preloader_mc.visible=true;
preloader_mc.play();
}
function inCaricamento(e:ProgressEvent):void
{
var percentuale:uint=(e.bytesLoaded/e.bytesTotal)*100;
preloader_mc.loader_txt.text=percentuale.toString()+' %';
}
function completato(e:Event):void
{
preloader_mc.stop();
preloader_mc.visible=false;
addChild(loader);
}
Let"s analyse the code:
I stop the Timeline of "preloader_mc"
preloader_mc.stop();
I render "preloader_mc" visible
preloader_mc.visible=false;
I create a variable "swf" which contains the url of the SWF to be loaded
var swf:String='http://www.flepstudio.org/swf/principianti/preloader/test.swf';
I create an URL request to which I pass the value of the variable "swf"
var richiesta:URLRequest=new URLRequest(swf);
I create a Loader
var loader:Loader=new Loader();
I add the listeners to the Loader"s property contentLoaderInfo which will listen to the 3 events during the loading: EventOPEN (loading started), ProgressEvent.PROGRESS (loading in process) and EventCOMPLETE (loading finished). At each events, a function will be called: inizia(), inCaricamento(),completato().
loader.contentLoaderInfo.addEventListener(Event.OP EN,inizia);
loader.contentLoaderInfo.addEventListener(Progress Event.PROGRESS,inCaricamento);
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE,completato);
I tell the Loader to load the request "richiesta"
loader.load(richiesta);
in this function I render "preloader_mc" visible and start its Timeline function inizia(e:Event):void
{
preloader_mc.visible=true;
preloader_mc.play();
}
in this function I calculate the percent loaded and view it via a dynamic text field include in "preloader_mc"
function inCaricamento(e:ProgressEvent):void
{
var percentuale:uint=(e.bytesLoaded/e.bytesTotal)*100;
preloader_mc.loader_txt.text=percentuale.toString( )+' %';
}
last, I stop the "preloader_mc" Timeline, render it invisible and add to the stage (using the method addChild) the Loader which now contains the external SWF function completato(e:Event):void
{
preloader_mc.stop();
preloader_mc.visible=false;
addChild(loader);
}
Source files: