ActionScript 3 AS3预加载器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3预加载器相关的知识,希望对你有一定的参考价值。

package
{
	import fl.controls.Label;
	import fl.controls.ProgressBar;
	import fl.controls.ProgressBarDirection;
	import fl.controls.ProgressBarMode;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.utils.getDefinitionByName;
	
	/**
	 * ...
	 * @author John
	 */
	public class Preloader extends MovieClip 
	{
		private var _loader:Sprite;
		private var _progressText:Label;
		private var _progressBar:ProgressBar;
		
		public function Preloader() 
		{
			if (stage) {
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
			}
			addEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// show loader
			showLoader();
		}
		
		private function ioError(e:IOErrorEvent):void 
		{
			trace(e.text);
		}
		
		private function progress(e:ProgressEvent):void 
		{
			// update loader
			var bytesLoaded:int = loaderInfo.bytesLoaded;
			var bytesTotal:int = loaderInfo.bytesTotal;
			
			var kiloBytesLoaded:int = bytesLoaded / 1024;
			var kiloBytesTotal:int = bytesTotal / 1024;
			
			_progressBar.setProgress(bytesLoaded, bytesTotal);
			_progressText.text = kiloBytesLoaded + "K / " + kiloBytesTotal + "K";			
		}
		
		private function checkFrame(e:Event):void 
		{
			if (loaderInfo.bytesLoaded == loaderInfo.bytesTotal) 
			{
				stop();
				loadingFinished();
			}
		}
		
		private function loadingFinished():void 
		{
			removeEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// hide loader
			removeChild(_loader);
			startup();
		}
		
		private function startup():void 
		{
			var mainClass:Class = getDefinitionByName("Main") as Class;
			if (parent == stage) stage.addChildAt(new mainClass() as DisplayObject, 0);
			else addChildAt(new mainClass() as DisplayObject, 0);
		}
		
		private function showLoader():void
		{
			_loader = new Sprite();
			
			_progressBar = new ProgressBar();
			_progressBar.direction = ProgressBarDirection.RIGHT;
			_progressBar.mode = ProgressBarMode.MANUAL;
			_progressBar.move(10, 10);
			_loader.addChild(_progressBar);
			
			_progressText = new Label();
			_progressText.move(10, 12);
			_loader.addChild(_progressText);
			
			addChild(_loader);			
		}
		
	}
	
}

以上是关于ActionScript 3 AS3预加载器的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3:预加载器代码

ActionScript 3 AS3:第1帧上的预加载器

ActionScript 3 带有圆形预加载器微调器的AS3 Image Loader

ActionScript 3 AS3:处理ie7中的预加载器

ActionScript 3 AS3视频播放器,放置在时间轴上,带有旋转预加载器

ActionScript 3 预加载器和进度结构