Flash CS3 - Random Image Loader With Progress Bar
ImageLoader.zip
package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.text.*;
import fl.controls.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
public class ImageLoader extends Sprite {
var imageLoader:Loader = new Loader();
var imagesArr:Array=new Array("image1.jpg","image2.jpg","image3.jpg","image4.jpg","image5.jpg");
var mc_ProgressBar:MovieClip = new MovieClip();
var StatusBar:MovieClip = new MovieClip();
var txtStatus:TextField = new TextField();
var LoadBtn = new Button();
public function ImageLoader():void {
addChild(imageLoader);
addChild(mc_ProgressBar);
addChild(LoadBtn);
LoadBtn.y=270;
LoadBtn.label ="Load Image";
LoadBtn.width = 80;
imageLoader.alpha=0;
DrawProgressBar();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
LoadBtn.addEventListener(MouseEvent.CLICK,loadHandler);
}
function completeHandler(event:Event):void {
var imageTween:Tween = new Tween(imageLoader, "alpha", Strong.easeOut, 0, 1, 3, true);
txtStatus.text ="Image has been loaded successfully";
}
function progressHandler(event:ProgressEvent):void {
var CurrentStatus:String = String(Math.round(event.bytesLoaded * 100 / event.bytesTotal));
txtStatus.text ="Loading "+ CurrentStatus + " %";
StatusBar.width=(event.bytesLoaded * 100 / event.bytesTotal)*2;
}
function loadHandler(event:MouseEvent ):void {
var index:Number = Math.floor(Math.random() * imagesArr.length);
imageLoader.load(new URLRequest("images/"+imagesArr[index]));
}
function DrawProgressBar():void {
mc_ProgressBar.x=100;
mc_ProgressBar.y=280;
StatusBar.graphics.beginFill(0xCCCCCC, 1);
StatusBar.graphics.drawRect(0, 0, 200,5);
StatusBar.graphics.endFill();
StatusBar.width=1;
mc_ProgressBar.addChild(StatusBar);
var StatusBox:MovieClip = new MovieClip();
StatusBox.graphics.lineStyle(1, 0x000000);
StatusBox.graphics.drawRect(0, 0, 200,5);
mc_ProgressBar.addChild(StatusBox);
mc_ProgressBar.addChild(txtStatus);
txtStatus.width =200;
txtStatus.y =-15;
}
}
}
Comments
Work from home India