Return to Snippet

Revision: 7171
at July 11, 2008 04:22 by gfazioli


Initial Code
/**
	 * Common class for loading an external flash or image
	 *
	 * @class			UndoLoader
	 * @author			Giovambattista Fazioli <[email protected]>
	 * @web				http://www.undolog.com
	 * @version			1.0
	 *
	 */
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.net.*;
	import flash.system.*;
	//
	public class UndoLoader extends MovieClip {
		private var _preloader:Preloader;
		private var _loader:Loader;
		private var _bar:MovieClip;
		private var _perc:TextField;
		
		/**
		 * Class constructor
		 *
		 * @param		void
		 * @return 		void
		 */
		public function UndoLoader() {
			stage.scaleMode		= StageScaleMode.NO_SCALE
			stage.align			= StageAlign.TOP_LEFT;
			addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		/**
		 * Init bar and content. Trigged when added to stage
		 *
		 * @param		{event}		e 	Passed from addEventListener(). Can be null
		 * @return 		void
		 * @private
		 */
		private function init(e:Event = null):void {
			initBar();
			loadContent();
		}

		/**
		 * Create the preloader movie and init it.
		 *
		 * @param		void
		 * @return 		void
		 * @private
		 */
		private function initBar():void {
			_preloader		= new Preloader();
			_bar			= _preloader.bar;
			_bar.scaleX		= 0;
			_perc			= _preloader.perc;
			_perc.text		= '0%';
			_preloader.x	= (stage.stageWidth - _preloader.width)/2;
			_preloader.y	= (stage.stageHeight - _preloader.height)/2;
			addChild( _preloader );
		}
		
		/**
		 * Creare a Loader() object and init PROGRESS and COMPLETE event
		 *
		 * @param		void
		 * @return 		void
		 * @private
		 */
		private function loadContent():void {
			_loader						= new Loader();
			_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, inProgress );
			_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
			_loader.load( new URLRequest( this.loaderInfo.parameters.filename ) );
			addChild( _loader );
		}

		/**
		 * Trigged when loading in progress. Move the progress bar and set 
		 * the text string
		 *
		 * @param		{ProgressEvent}		e	ProgressEvent
		 * @return 		void
		 * @private
		 */
		private function inProgress(e:ProgressEvent = null):void {
			var p:Number	= Math.floor( (e.bytesLoaded / e.bytesTotal)*100 );
			_perc.text 		= p + '%';
			_bar.scaleX		= p/100;
		}

		/**
		 * Trigged when loading is finished. Remove the preloader movie
		 * from the stage. Get the framerate from loaded movie and set
		 * framerate to main movie.
		 *
		 * @param		{Event}		e	Event
		 * @return 		void
		 * @private
		 */
		private function onComplete(e:Event = null):void {
			stage.frameRate = _loader.contentLoaderInfo.frameRate;
			removeChild( _preloader );
		}
	}

Initial URL
http://www.undolog.com/2008/06/16/creare-un-preloader-generico-in-actionscript-30/

Initial Description
See also: http://code.google.com/p/undolibrary/

Initial Title
Common class for loading and preloading an external flash or image

Initial Tags
flash

Initial Language
ActionScript 3