In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: code on 2009-1-9

Author: Richard Owen
Based on some code i saw along time ago and can not find now to reference
Move your mouse around the stage and check out the water trail
this works well as rain drops if you want to randomly drop points onto the surface
Known Isuues: THe water
// forked from Metrix's code on 2009-1-9
// Author: Richard Owen
// Based on some code i saw along time ago and can not find now to reference
// Move your mouse around the stage and check out the water trail
// this works well as rain drops if you want to randomly drop points onto the surface
//
// Known Isuues: THe water

// write as3 code here..
package 
{

	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	import flash.utils.setInterval;
	import flash.utils.clearInterval;
	
	public class temp extends Sprite 
	{
		private var Water:WaterMaterial;

		private var intervalID:uint = 0;

		
		public function temp():void 
		{
			trace(1);
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
			trace(2);
		}
		
		private function init(e:Event = null):void 
		{


			removeEventListener(Event.ADDED_TO_STAGE, init);
	
			
			Water = new WaterMaterial();
			stage.addChild(Water);
	
			Water.addEventListener(MouseEvent.MOUSE_DOWN, MouseClicky)
			Water.addEventListener(MouseEvent.MOUSE_UP, MouseRelease)
			Water.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove)

			intervalID = setInterval(WaterUpdate, 50);
			

		}
		
		private function MouseClicky(event:MouseEvent):void {
			Water.setDrop(event.localX, event.localY);
		};
		
		private function MouseMove(event:MouseEvent):void {
			Water.setDrop(event.localX, event.localY);
		};

		
		public function WaterUpdate():void { 
			Water.forceRefresh();
		}
		
		private function MouseRelease(event:MouseEvent):void {
			//clearInterval(intervalID);
		};

	}
}
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.InteractiveObject;
	import flash.geom.Rectangle;
	import flash.display.Sprite;
	import flash.events.Event;

	class WaterMaterial extends Sprite
	{
		private const TestScale:Number = 5.0;
		private const BufWidth:uint = 93;	
		private const BufHeight:uint = 93;
		private const MaxHeight:uint = 256;
		
		private const damping:Number = 0.90;
		
		private var Buffer1:Array = new Array(BufWidth);
		private var Buffer2:Array = new Array(BufWidth);
		private var tempBuff:Array;
		
		private var waterData:BitmapData;
		private var waterMap:Bitmap;
		
		public function WaterMaterial() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(e:Event): void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			//init code goes here
			
			waterData = new BitmapData(BufWidth, BufHeight, true, 0x00000000);
			waterMap = new Bitmap(waterData);
			addChild(waterMap);
			
			this.x = 0;
			this.y = 0;
			this.width = BufWidth * TestScale;
			this.height = BufHeight * TestScale;
			
			var col:int;
			var row:int;
			for (col = 0; col < BufWidth ; col++ ) {
  			    Buffer1[col] = new Array(BufHeight);
				for (row = 0; row < BufHeight; row++) {
					Buffer1[col][row] = 0;
				}
			}	
			for (col = 0; col < BufWidth ; col++ ) {
  			    Buffer2[col] = new Array(BufHeight);
				for (row = 0; row < BufHeight; row++) {
					Buffer2[col][row] = 0;
				}
			}	
			
		}
		
		public function setDrop(dropX:uint, dropY:uint):void {
			var col:int;
			var row:int;
			
  			Buffer1[dropX][dropY] = MaxHeight;

			for (col = dropX - 1; col < dropX + 1; col++)
			  for (row = dropY - 1; row < dropY + 1; row++) {
  			    //Buffer1[col][row] = MaxHeight
			  }
		}
				
		public function updateRipple():void {

			var col:int;
			var row:int;
			var pixelColor:uint;
			
			for (col = 1; col < BufWidth - 2; col++ ) {
				for (row = 1; row < BufHeight - 2; row++) {
					var oldVal:uint = Buffer2[col][row];
					
					Buffer2[col][row] = ((Buffer1[col - 1][row] + 
					                      Buffer1[col + 1][row] + 
										  Buffer1[col][row + 1] + 
										  Buffer1[col][row - 1] +
										  Buffer1[col - 1][row - 1] +
										  Buffer1[col - 1][row + 1] +
										  Buffer1[col + 1][row - 1] +
										  Buffer1[col + 1][row + 1]  ) / 4) - oldVal;
					
					
										  
					var newVal:int = Buffer2[col][row];
					
					Buffer2[col][row] = Math.floor(newVal * damping);
					
					
					
					if (Buffer2[col][row] < 0) Buffer2[col][row] = 0;

					//if (Buffer2[col][row] > MaxHeight) Buffer2[col][row] = MaxHeight;
					pixelColor = Buffer2[col][row] << 24;
					pixelColor = pixelColor + 0xff;
					waterData.setPixel32(col + 1, row + 1, pixelColor);
				}
			}
		}
		
		public function forceRefresh(): void {
			
			//Time the update
			updateRipple();
			
			var col:int;
			var row:int;
			var pixelColor:uint;
			
			waterData.lock();
			
			
			for (col = 0; col < BufWidth ; col++ ) {
				for (row = 0; row < BufHeight; row++) {			
					pixelColor = Buffer2[col][row];

				}
			}
			
			for (col = 0; col < 2 ; col++ ) {
				for (row = 0; row < 2; row++) {			
				}
			}
			
			
			waterData.unlock();
			
			tempBuff = Buffer2;
			Buffer2 = Buffer1;
			Buffer1 = tempBuff;
		}
	}