This is an article which could be a variant of these articles:
Loading an external SWF with Flash CS3
Preloader with Flash CS3 and
Preloader for beginners
In this case, I use the ProgressBar component to monitor the loading state of an external SWF.
Also, I use the IULoader component to load the SWF instead of the Loader class (used in many examples in this site) and a Label component instead of a dynamic text field.
Let's see how it works'
I create a FLA and save it as 'bar.fla', into which I drag a instance of the ProgressBar component and give it an name 'br_pb'.
I create a Document Class, an ASfile saved as 'Main.as', implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import fl.containers.UILoader;
import fl.controls.Label;
import flash.text.TextFieldAutoSize;
import flash.events.Event;
import flash.events.ProgressEvent;
public class Main extends MovieClip
{
private var UI_loader:UILoader
private var etichetta:Label;
private var url:String;
public function Main()
{
init();
}
private function init():void
{
url='http://www.flepstudio.org/swf/my_swf.swf'cachebuster='+new Date().getTime();
UI_loader=new UILoader();
UI_loader.autoLoad=false;
UI_loader.source=url;
UI_loader.move(stage.stageWidth/2,stage.stageHeight/2);
UI_loader.scaleContent=false;
UI_loader.load();
bar_pb.source=UI_loader;
bar_pb.move(stage.stageWidth/2-bar_pb.width/2,stage.stageHeight/2);
bar_pb.addEventListener(ProgressEvent.PROGRESS,progresso);
bar_pb.addEventListener(Event.COMPLETE,completato);
etichetta=new Label();
etichetta.autoSize=TextFieldAutoSize.LEFT;
etichetta.move(bar_pb.x,bar_pb.y+bar_pb.height);
addChild(etichetta);
}
private function progresso(e:ProgressEvent):void
{
etichetta.text=int(e.currentTarget.percentComplete)+'%';
}
private function completato(e:Event):void
{
bar_pb.removeEventListener(ProgressEvent.PROGRESS,progresso);
bar_pb.removeEventListener(Event.COMPLETE,completato);
removeChild(bar_pb);
removeChild(etichetta);
addChild(UI_loader);
UI_loader.move(-100,-100);
}
}
}
The result:
Let's analise the code.
Properties
an instance of the UILoader component
private var UI_loader:UILoader
an instance of the Label component
private var etichetta:Label;
a string variable containing the URL of the external SWF
private var url:String;
Methods
init();
I assign the external SWF url to the variable 'url'
url='http://www.flepstudio.org/swf/my_swf.swf'cachebuster='+new Date().getTime();
I create an instance of UILoader component
UI_loader=new UILoader();
I impost its autoLoad property to false
UI_loader.autoLoad=false;
I assign to its source property, the value of the variable 'url'
UI_loader.source=url;
I position the component
UI_loader.move(stage.stageWidth/2,stage.stageHeight/2);
I impost its scaleContent property to false
UI_loader.scaleContent=false;
I start the loading of the external SWF
UI_loader.load();
I tell the ProgressBar that its source property is the instance of UILoader
bar_pb.source=UI_loader;
I position the ProgressBar
bar_pb.move(stage.stageWidth/2-bar_pb.width/2,stage.stageHeight/2);
I add a listener to the ProgressBar to the ProgressEvent.PROGRESS, which will call the function progresso()
bar_pb.addEventListener(ProgressEvent.PROGRESS,pro gresso);
I add a listener to the ProgressBar to the Event.COMPLETE, which will call the function completato()
bar_pb.addEventListener(Event.COMPLETE,completato) ;
I create an instance of the Label component
etichetta=new Label();
I assign an autosize, using the static method LEFT of the TextFieldAutoSize class
etichetta.autoSize=TextFieldAutoSize.LEFT;
I position the Label
etichetta.move(bar_pb.x,bar_pb.y+bar_pb.height);
I add it to the DisplayObject (otherwise it would not be visible)
addChild(etichetta);
progresso();
I assign the text to the Label component. In this case, the percentComplete property of the ProgressBar
etichetta.text=int(e.currentTarget.percentComplete )+'%';
completato();
I remove the listeners
bar_pb.removeEventListener(ProgressEvent.PROGRESS, progresso);
bar_pb.removeEventListener(Event.COMPLETE,completa to);
I remove the ProgressBar and Label components
removeChild(bar_pb);
removeChild(etichetta);
I add to the DisplayObject the instance of the UILoader which now contains the external SWF and is a MovieClip
addChild(UI_loader);
I position the instance of UILoader
UI_loader.move(-100,-100);
Source files: