package {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.*
import flash.text.*;
public class Main extends Sprite {
private var loader:Loader; // The asset loader
private var progressOutput:TextField; // The text field in which
// to display load progress
public function Main () {
createLoader();
createProgressIndicator();
load(new URLRequest("sunset.jpg"));
}
private function createLoader ( ):void {
loader = new Loader( );
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,
progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
completeListener);
loader.contentLoaderInfo.addEventListener(Event.INIT,
initListener);
}
private function createProgressIndicator ( ):void {
progressOutput = new TextField( );
progressOutput.autoSize = TextFieldAutoSize.LEFT;
progressOutput.border = true;
progressOutput.background = true;
progressOutput.selectable = false;
progressOutput.text = "LOADING...";
}
private function load (urlRequest:URLRequest):void {
loader.load(urlRequest);
if (!contains(progressOutput)) {
addChild(progressOutput);
}
}
// Listener invoked whenever data arrives
private function progressListener (e:ProgressEvent):void {
// Update progress indicator.
progressOutput.text = "LOADING: "
+ Math.floor(e.bytesLoaded / 1024)
+ "/" + Math.floor(e.bytesTotal / 1024) + " KB";
}
private function initListener (e:Event):void {
addChild(loader.content); // Add loaded asset to display list
}
private function completeListener (e:Event):void {
removeChild(progressOutput);
}
}
}
|