back to devpro   download
/**
 * Class Zoomer :: Zoomer.as :: by Andrea Giammarchi [ andr3a ]
 * Preloads a list of numeric jpg images and zoom them.
 * Thanks to http://zoomquilt.nikkki.net/ and THE ZOOM QUILT project [ http://zoomquilt.nikkki.net/ ]
 * You can see an example at this address: http://www.3site.it/caxxate/PROVA/ZOOM/zoom.html
 * --------------------------------------------------
 * EXAMPLE:
 *		stop();
 *
 * 		var myZoomer:Zoomer = new Zoomer( this, '', 1, 46 );
 * 
 * 
 * 		var keyListener:Object = new Object();
 * 		keyListener.onKeyDown = function():Void {
 *			var incrementSpeed:Number = Number( 0.1 );
 * 			if( myZoomer.getSpeed() + incrementSpeed < 10 && Key.getCode() == 38 ) {
 * 				myZoomer.speed( '+', 0.1 );
 * 			}
 * 			else if( myZoomer.getSpeed() - incrementSpeed > -10 && Key.getCode() == 40 ) {
 * 				myZoomer.speed( '-', 0.1 );
 * 			}
 * 		}
 *
 * 	this.onEnterFrame = function():Void {
 * 		if( myZoomer.canStart() == true ) {
 * 			Key.addListener( keyListener );
 * 			delete this.onEnterFrame;
 * 		}
 * 	}
 * NOTE: images in folder must be called in this way:
 *		1.jpg - 2.jpg - ... - 10.jpg - ... - 23.jpg - ... - XXX.jpg
 * --------------------------------------------------
 * @type	ActionScript 2.0 class file
 * @version	1.0 stable
 * @author	Andrea Giammarchi
 * @date	29/10/2004
 * @lastMod	29/10/2004 16:15
 * @site	http://www.3site.it/
 */
class Zoomer {
	
	/**
	 * private varibles
	 *		__canstart		Boolean		defalut: false [ used from swf to know if speed is changable ]
	 *		__speed			Number		defalut: 0 [ zoom speed ]
	 *		__remmovement	Boolean		defalut: true [ check movements, zoomIn or zoomOut ]
	 *		__path			Object		where this class will create animation ( use this or _levelX )
	 *		__barloader		MovieClip	Movie created by this class to show a preload
	 *		__maskmovie		MovieClip	Movie created by this class to mask images
	 *		__imagesfolder	String		where there are images
	 *		__clips			Array		internal movieClips container
	 */
	private var __canstart:Boolean = new Boolean( false );
	private var __speed:Number = new Number( 0 );
	private var __remmovement:Boolean = new Boolean( true );
	private var __path:Object;
	private var __barloader:MovieClip;
	private var __maskmovie:MovieClip;
	private var __imagesfolder:String;
	private var __clips:Array = new Array();
	
	/**
	 * public constructor
	 *		Assigns path and folder, calls __createLoader method and __imgloader method too.
	 *			new Zoomer( path:Object, imagesFolder:String, __startimg:Number, __endimg:Number );
	 * @param	Object		Path where animation will be created ( use this or _levelX for example )
	 * @param	String		Folder where there are numeric .jpg images ( 1.jpg, 2.jpg ... 30.jpg ... XXX.jpg ... )
	 * @param	Number		First valid numeric image name		
	 * @param	Number		Last numeric image name			
	 */
	function Zoomer( __path:Object, __imagesfolder:String, __startimg:Number, __endimg:Number ) {
		this.__path = __path;
		this.__imagesfolder = __imagesfolder;
		this.__createLoader();
		this.__imgloader( __startimg, __endimg );
	}
	
	// PUBLIC METHODS
	
		/**
		 * public method
		 *		just returns private __canstart variable to manage, from swf, the animation speed
		 *			Zoomer.canStart():Boolean
		 * @param	none
		 * @return	Boolean		private __canstart value	
		 */
		public function canStart():Boolean {
			return this.__canstart;
		}
		
		/**
		 * public method
		 *		removes all movieClips created or loaded for animation
		 *			Zoomer.removeZoom():Void
		 * @param	none
		 * @return	none
		 */
		public function removeZoom():Void {
			for( var a:Number = 0; a < this.__clips.length; a++ ) {
				this.__clips[a].removeMovieClip();
			}
		}
		
		/**
		 * public method
		 *		increases or decreases internal __speed value 
		 *			Zoomer.speed( type:String, adjust:Number ):Void
		 * @param	String		Accepted values: 'PLUS' , 'UP' and '+' - 'MINUS' , 'DOWN' and '-'
		 * @param	Number		amount for speed increment ( 0.1, 0.5, 2 ... )
		 * @return	none
		 *
		 */
		public function speed( type:String, adjust:Number ):Void {
			if( type.toUpperCase() == 'PLUS' || type.toUpperCase() == 'UP' || type == '+' ) {
				this.__speed += adjust;
				
			}
			else if( type.toUpperCase() == 'MINUS' || type.toUpperCase() == 'DOWN' || type == '-' ) {
				this.__speed -= adjust;
			}
		}
		
		/**
		 * public method
		 *		just returns private getSpeed variable
		 *			Zoomer.getSpeed():Number
		 * @param	none
		 * @return	Number		internal __speed value
		 *
		 */
		public function getSpeed():Number {
			return this.__speed;
		}
	
	// PRIVATE METHODS
	
		/**
		 * private method
		 *		Set __barloader movieclip to start preload for all images.
		 *		Calls __zoomImage private method at the end of preload ( gives them last image number )
		 *			Zoomer.__startOrder():Void
		 * @param	none
		 * @return	none
		 *
		 */
		private function __startOrder():Void {
			this.__barloader.path = this;
			this.__barloader.lastImage = this.__clips[(this.__clips.length-1)];
			this.__barloader.onEnterFrame = function():Void {
				var bL:Number = this.path.__dataLoaded();
				var tL:Number = this.path.__dataTotal();
				if( tL > 0 ) {
					var perc:Number = Math.ceil( ( bL / tL ) * 100 );
					this['__bar__']._xscale += ( ( perc - this['__bar__']._xscale ) * 0.2 );
					if( bL >= tL && this['__bar__']._xscale >= 99 && this.lastImage.getBytesTotal() > 0 ) {
						this['__bar__']._xscale = 100;
						delete this['__end__'].onEnterFrame;
						this['__end__']._visible = true;
						this.onEnterFrame = function():Void {
							if( this._alpha > 1 ) {
								this._alpha -= 2;
							}
							else {
								var __npath:Object = this.path;
								this.lastImage._alpha = 0;
								this.lastImage._visible = true;
								this.lastImage.onEnterFrame = function() {
									this._alpha += 2;
									if( this._alpha >= 99 ) {
										this._alpha = 100;
										delete this.onEnterFrame;
										__npath.__zoomImage( __npath.__clips.length - 1 );
									}
								}
								delete this.onEnterFrame;
								this._alpha = 0;
								this.removeMovieClip();
							}
						}
					}
				}
				
			}
		}
		
		/**
		 * private method
		 *		Loops over internal __clips container and returns all images bytes loaded.
		 *		Sets image visibility to false too.
		 *			Zoomer.__dataLoaded():Number
		 * @param	none
		 * @return	Number		all bytes loaded
		 *
		 */
		private function __dataLoaded():Number {
			var bLoaded:Number = Number( 0 );
			for( var a:Number = 0; a < this.__clips.length; a++ ) {
				var tLoaded:Number = this.__clips[a].getBytesLoaded();
				bLoaded += tLoaded;
				if( tLoaded >= this.__clips[a].getBytesTotal() && this.__clips[a]._visible == true ) {
					this.__clips[a]._visible = false;
				}
			}
			return bLoaded;
		}
		
		/**
		 * private method
		 *		Loops over internal __clips container and returns all images total bytes.
		 *			Zoomer.__dataTotal():Number
		 * @param	none
		 * @return	Number		all bytes to load
		 *
		 */
		private function __dataTotal():Number {
			var bTotal:Number = Number( 0 );
			for( var a:Number = 0; a < this.__clips.length; a++ ) {
				bTotal += this.__clips[a].getBytesTotal();
			}
			return bTotal;
		}
		
		/**
		 * private method
		 *		Assigns mask for incoming image to show, centers them with private __centerImage method.
		 *		Starts movieClip onEnterFrame with all checks to zoom in or zoom out image.
		 *			Zoomer.__zoomImage( image:Number ):Void
		 * @param	Number		image number
		 * @return	none		
		 *
		 */
		private function __zoomImage( image:Number ):Void {
			if( this.__maskmovie.setted == false ) {
					this.__maskmovie._width = this.__clips[image]._width;
					this.__maskmovie._height = this.__clips[image]._height;
					this.__maskmovie.setted = true;
			}
			this.__clips[image].setMask( this.__maskmovie );
			this.__centerImage( this.__clips[image] );
			this.__clips[image].path = this;
			this.__clips[image]._visible = true;
			this.__clips[image].onEnterFrame = function():Void {
				if( this.path.__canstart == false ) {
					if( this.path.__speed < 1 ) {
						this.path.__speed += 0.01;
					}
					else {
						this.path.__speed = 1;
						this.path.__canstart = true;
					}
				}
				this._xscale = this._yscale = this._xscale + ( this.path.__speed * ( this._xscale / 50 ) );
				this.path.__centerImage( this );
				if( this.path.__speed >= 0 ) {
					this.path.__reverseZooming( this, 100, false, true );
					if( this._xscale > 199.2 ) {
						if( image - 1 >= 0 ) {
							this.path.__zoomImage( --image );
						}
						else {
							this.path.__zoomImage( this.path.__clips.length - 1 );
						}
						this.path.__stopZooming( this, 100 );
					}
				}
				else {
					this.path.__reverseZooming( this, 200, true, false );
					if( this._xscale < 100.8 ) {
						if( image + 1 < this.path.__clips.length ) {
							this.path.__zoomImage( ++image );
						}
						else {
							this.path.__zoomImage( 0 );
						}
						this.path.__stopZooming( this, 200 );
					}
				}
			}
		}
		
		/**
		 * private method
		 *		Used inside __zoomImage private method to check if zoom was reversed, then set internal 
		 *		__remmovement boolean variable and reset image too.
		 *			Zoomer.__reverseZooming( image:MovieClip, newScale:Number, cD:Boolean , nD:Boolean ):Void
		 * @param	MovieClip	movieClip to reset after check
		 * @param	Number		new scale for MovieClip ( used from internal __resetStatus method )
		 * @param	Boolean		boolean value to compare with internal __remmovement var
		 * @param	Boolean		new boolean value to assigns to internal __remmovement var
		 * @return	none		
		 *
		 */
		private function __reverseZooming( image:MovieClip, newScale:Number, cD:Boolean , nD:Boolean ):Void {
			if( this.__remmovement == cD ) {
				this.__remmovement = nD;
				this.__resetStatus( image, newScale );
			}
		}
		
		/**
		 * private method
		 *		Used inside __zoomImage private method to delete onEnterFrame, center image and reassign x and y position 
		 *			Zoomer.__stopZooming( image:MovieClip, newScale:Number ):Void
		 * @param	MovieClip	movieClip to manage
		 * @param	Number		new scale for MovieClip
		 * @return	none		
		 *
		 */
		private function __stopZooming( image:MovieClip, newScale:Number ):Void {
			delete image.onEnterFrame;
			image._visible = false;
			image._xscale = image._yscale = newScale;
			this.__centerImage( image );
		}
		
		/**
		 * private method
		 *		Used inside __zoomImage private method to reset old settings for all images. 
		 *			Zoomer.__resetStatus( dontReset:MovieClip, newScale:Number ):Void
		 * @param	MovieClip	movieClip that will not be resetted
		 * @param	Number		new scale for MovieClip
		 * @return	none		
		 *
		 */
		private function __resetStatus( dontReset:MovieClip, newScale:Number ):Void {
			for( var a:Number = 0; a < this.__clips.length; a++ ) {
				if( this.__clips[a] != dontReset ) {
					this.__clips[a]._xscale = this.__clips[a]._yscale = newScale;
					this.__centerImage( this.__clips[a] );
				}
			}
		}
		
		/**
		 * private method
		 *		Used inside __zoomImage private method to center image with new scale proprieties. 
		 *			Zoomer.__centerImage( image:MovieClip ):Void
		 * @param	MovieClip	movieClip that will be centered
		 * @return	none		
		 *
		 */
		private function __centerImage( image:MovieClip ):Void {
			if( image.__remX == undefined ) {
				image.__remX = image._x;
				image.__remY = image._y;
			}
			var iS:Number = Number( image._xscale );
			var iW:Number = Number( image._width );
			var iH:Number = Number( image._height );
			image._xscale = image._yscale = 100;
			var oW:Number = Number( image._width );
			var oH:Number = Number( image._height );
			var ab:Number = ( iW - oW ) / 2;
			var bc:Number = ( iH - oH ) / 2;
			image._xscale = image._yscale = iS;
			image._x = image.__remX - ab;
			image._y = image.__remY - bc;
		}
		
		/**
		 * private method
		 *		Creates as movies as there are from  __startimg and __endimg.
		 *		Then does a loadMovie for all images and push new movie on internal __clips container.
		 *			Zoomer.__imgloader( __startimg:Number, __endimg:Number ):Void
		 * @param	Number		first valid numeric image name to load
		 * @param	Number		last valid numeric image name to load
		 * @return	none		
		 *
		 */
		private function __imgloader( __startimg:Number, __endimg:Number ):Void {
			for( var a:Number = __startimg; a <= __endimg; a++ ) {
				var mcName:String = '__ZoomerClip__' + a + '__' + String( Math.round( Math.random() * 1000000 ) );
				this.__path.createEmptyMovieClip( mcName, this.__path.getNextHighestDepth() );
				this.__path[mcName].loadMovie( this.__imagesfolder + '' + a + '.jpg' );
				this.__clips.push( this.__path[mcName] );
			}
			this.__startOrder();
		}
		
		/**
		 * private method
		 *		Creates __barloader and __maskmovie .
		 *			Zoomer.__createLoader():Void
		 * @param	none
		 * @return	none		
		 *
		 */
		private function __createLoader():Void {
			var mcName:String = '__Zoomer__' + String( Math.round( Math.random() * 1000000 ) ) + '__';
			this.__path.createEmptyMovieClip( mcName, this.__path.getNextHighestDepth() );
			this.__barloader = this.__path[mcName];
			this.__barloader.createEmptyMovieClip( '__bar__', this.__barloader.getNextHighestDepth() );
			this.__barloader.createEmptyMovieClip( '__end__', this.__barloader.getNextHighestDepth() );
			with( this.__barloader['__bar__'] ) {
				moveTo( 4, Stage.height - 8 );
				beginFill( 0xEFEFEF , 50 );
				lineTo( Stage.width - 16, Stage.height - 8 );
				lineTo( Stage.width - 16, Stage.height - 8 + 4 );
				lineTo( 4, Stage.height - 8 + 4 );
				lineTo( 4, Stage.height - 8 );
				endFill();
			}
			with( this.__barloader['__end__'] ) {
				moveTo( Stage.width - 8, Stage.height - 8 );
				beginFill( 0xFFFFFF , 100 );
				lineTo( Stage.width - 4, Stage.height - 8 );
				lineTo( Stage.width - 4, Stage.height - 8 + 4 );
				lineTo( Stage.width - 8, Stage.height - 8 + 4 );
				lineTo( Stage.width - 8, Stage.height - 8 );
				endFill();
			}
			this.__barloader['__bar__']._xscale = 0;
			this.__barloader['__end__'].onEnterFrame = function():Void {
				this._visible = !this._visible;
			}
			mcName = '__Zoomer__Mask__' + String( Math.round( Math.random() * 1000000 ) ) + '__';
			this.__path.createEmptyMovieClip( mcName, this.__path.getNextHighestDepth() );
			this.__maskmovie = this.__path[mcName];
			with( this.__maskmovie ) {
				moveTo( 0, 0 );
				beginFill( 0x000000 , 100 );
				lineTo( 1, 0 );
				lineTo( 1, 1 );
				lineTo( 0, 1 );
				lineTo( 0, 0 );
				endFill();
			}
			this.__maskmovie.setted = false;
		}
}
back to devpro   download
Creative Commons License