back to devpro   download
/**
 * Class WaterEffect :: WaterEffect.as :: by Andrea Giammarchi
 * This class generates a kind of water effect, with mouse movements
 * or with automatic generation.
 * __________________________________________________
 * EXAMPLE WITH MOUSE MOVEMENT:
 * 		var waterEffect:Object = new WaterEffect( this, 'FISH' );
 * 		Mouse.addListener( waterEffect );
 *
 * EXAMPLE WITH AUTOMATIC GENERATION:
 * 		var waterEffect:Object = new WaterEffect( this, 'FISH' );
 * 		waterEffect.start( 30 );
 * --------------------------------------------------
 * @type	ActionScript 2.0 class file
 * @version	1.0 ( CPU centrino 1.6 tested )
 * @author	Andrea Giammarchi
 * @date	02/04/2005
 * @lastMod 02/04/2005 09:00
 * @site	http://www.devpro.it/
 */
class WaterEffect {
	
	/** private variables */
	private var CopyRight:String = 'Andrea Giammarchi [ www.3site.it ]';
	
	private var __diameter:Number;						// circles diameter
	private var __levelSaver:Number;					// moveiLevel counter
	private var __intervall:Number = new Number( 0 );	// internal interval
	private var __math:Object;							// internal Math object ( faster! )
	private var __path:Object;							// internal path remember
	
	/**
	 * public constructor
	 * 		new WaterEffect( pathReferer:Object, imageLinkage:String[, circleDiameter:Number] )
	 * @param		Object		path where you want to create a water effect 
	 *							( level or MovieClip )
	 * @param		String		the name of the linkage of the image to waterEffect
	 *							( linkage an image movieClip and export for ActionScript )
	 * @param		Number		the diameter of generated circles [ DEFAULT: 8 ]
	 *							( smallest => faster effect, try with 4 )
	 */
	function WaterEffect( __path:Object, __id:String, __diameter:Number ) {
		this.__diameter = __diameter == undefined ? 8 : __diameter;
		this.__math = Math;
		this.__path = __path;
		this.__createMask( __id );
	}
	
	/**
	 * public method
	 *	Starts automatic water effect with a frequency to generate circles.
	 *	NOTE: frequency should be in a range 20 <= frequency <= 50
	 *
	 * 		this.start( frequency:Number ):Void
	 *
	 * @param		Number		interval to generate random circles
	 * @return		Void
	 */
	public function start( frequency:Number ):Void {
		if( this.__intervall > 0 ) {
			clearInterval( this.__intervall );
		}
		this.__intervall = setInterval( this, '__waterEffect', frequency, true );
	}
	
	/**
	 * public method
	 *	Delete automatic water effect
	 *
	 * 		this.end():Void
	 *
	 * @return		Void
	 */
	public function end():Void {
		clearInterval( this.__intervall );
		this.__intervall = 0;
	}

	/**
	 * public method
	 *	Called from Mouse event if it's used as Mouse listener.
	 * 	( for example: Mouse.addListener( waterObject ) )
	 *	Generates water effect on every mouse movement
	 *	NOTE: use this method with a framerate range < 60 or CPU will be hot!
	 *
	 * 		this.onMouseMove():Void
	 *
	 * @return		Void
	 */
	public function onMouseMove():Void {
		if( this.__intervall > 0 ) {
			clearInterval( this.__intervall );
		}
		this.__waterEffect( false );
	}
	
	/**
	 * private methods ( uncommented, sorry )
	 * 		Used to generate all movieClips that will create a water effect
	 */
	private function __nextDepth():Number {
		return this.__path['__water_effect__']['imask'].getNextHighestDepth();
	}
	
	private function __waterEffect( randomMouse:Boolean ):Void {
		var x:Number;
		var y:Number;
		if( randomMouse === false ) {
			x = this.__path._xmouse;
			y = this.__path._ymouse;
		}
		else {
			x = this.__math.round( this.__math.random() * this.__path['__water_mask__']._width );
			y = this.__math.round( this.__math.random() * this.__path['__water_mask__']._height );
		}
		var nd:Number = this.__nextDepth();
		var mname:String = 'circle_' + String( nd );
		this.__path['__water_effect__']['imask'].createEmptyMovieClip( mname, nd );
		this.__path['__water_effect__']['imask'][mname]._x = x;
		this.__path['__water_effect__']['imask'][mname]._y = y;
		this.__path['__water_effect__']['imask'][mname].__diameter = this.__diameter;
		this.__path['__water_effect__']['imask'][mname].__line = 
		this.__math.ceil( this.__math.random() * this.__diameter );
		this.__path['__water_effect__']['imask'][mname].__moveInterval = 
		setInterval( this, '__move', 20, this.__path['__water_effect__']['imask'][mname] );
	}
	
	private function __remove( who:MovieClip ):Void {
		clearInterval( who.__moveInterval );
		who.swapDepths( ( this.__levelSaver + 1 ) );
		who.removeMovieClip();
	}
	
	private function __move( who:MovieClip ):Void {
		var __or:Number = who.__diameter;
		var __ir:Number = who.__diameter - who.__line;
		who.clear();
		with( who ) {
			beginFill( 0x000000, 100 );
			moveTo( 0, __or );
			curveTo(__or, __or, __or, 0);
			curveTo(__or, -__or, 0, -__or);
			curveTo(-__or, -__or, -__or, 0);
			curveTo(-__or, __or, 0, __or);
			moveTo(0, __ir);
			curveTo(-__ir, __ir, -__ir, 0);
			curveTo(-__ir, -__ir, 0, -__ir);
			curveTo(__ir, -__ir, __ir, 0);
			curveTo(__ir, __ir, 0, __ir);
			endFill();
		}
		if( who.__line <= 0 ) {
			this.__remove( who );
		}
		else {
			who.__diameter += 0.5;
			who.__line -= 0.05;
		}
	}
	
	private function __createMask( __id:String ):Void {
		this.__levelSaver = this.__path.getNextHighestDepth();
		this.__path.createEmptyMovieClip( '__water_effect__', this.__levelSaver );
		this.__path['__water_effect__'].createEmptyMovieClip( 'image', this.__levelSaver++ );
		this.__path['__water_effect__'].createEmptyMovieClip( 'imask', this.__levelSaver++ );
		this.__path['__water_effect__'].createEmptyMovieClip( 'umask', this.__levelSaver++ );
		this.__path.createEmptyMovieClip( '__water_mask__', this.__levelSaver++ );
		this.__path['__water_effect__']['image'].attachMovie( __id, __id, this.__levelSaver++ );
		this.__path['__water_effect__']['umask'].attachMovie( __id, __id, this.__levelSaver++ );
		this.__path['__water_mask__'].attachMovie( __id, __id, this.__levelSaver++ );
		var __rx:Number = this.__path['__water_effect__']['umask'][__id]._width;
		var __ry:Number = this.__path['__water_effect__']['umask'][__id]._height;
		this.__path['__water_effect__']['umask'][__id]._xscale = 102;
		this.__path['__water_effect__']['umask'][__id]._yscale = 102;
		__rx = ( this.__path['__water_effect__']['umask'][__id]._width - __rx ) / 2;
		__ry = ( this.__path['__water_effect__']['umask'][__id]._height - __ry ) / 2;
		this.__path['__water_effect__']['umask'][__id]._x -= __rx;
		this.__path['__water_effect__']['umask'][__id]._y -= __ry;
		this.__path['__water_effect__'].setMask( this.__path['__water_mask__'] );
		this.__path['__water_effect__']['umask'].setMask( this.__path['__water_effect__']['imask'] );
	}
}
back to devpro   download
Creative Commons License