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

Water particle

Get Adobe Flash player
by Nasc 07 Apr 2010
/**
 * Copyright Nasc ( http://wonderfl.net/user/Nasc )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/qq99
 */

package {
    import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
    public class FlashTest extends Sprite {
    	private var _scene				: Sprite;
		private var _sceneBmpData		: BitmapData;
		private var _sceneBmp			: Bitmap;
		private var _sw					: int;
		private var _sh					: int;
		
		private var _countPart			: int = 10000;
		private var _partVector			: Vector.<Object>;
		
		private var _blur				: BlurFilter;
		private var _cache				: BitmapData;
		
		private var _txt				: TextField;
		
		private var _pixColor			: uint = 0xF4E768;
		private var _isOver				: Boolean = false;
		
		
		
		private var _someStupidBoolean	: Number = 0;
		
		private var _animationType		: String = "fall";
		private var _rewindCount		: int;
		
		private static var STARTX		: int = 235;
		private static var STARTY		: int = 0;
		private static var START_WIDTH	: int = 30;
        public function FlashTest() {
            // write as3 code here..
            addEventListener(Event.ADDED_TO_STAGE, init);
            
        }
        private function init(e:Event):void
		{
			var mx:int = mouseX;
			var my:int = mouseY;
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			addEventListener(MouseEvent.MOUSE_OVER, onMouseInteraction);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseInteraction);
			//addEventListener(MouseEvent.CLICK, onMouseInteraction);
			
			
			
			_scene = new Sprite();
			addChild(_scene);
			
			_blur = new BlurFilter(2, 30,1);			
			
			_sceneBmpData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF);
			_sceneBmp = new Bitmap(_sceneBmpData);
			_scene.addChild(_sceneBmp);
			
			_cache =  new BitmapData(stage.stageWidth, stage.stageHeight,true, 0xCC000000);
			
			_sw = stage.stageWidth;
			_sh = stage.stageHeight;
			
			var pixelBitmap:BitmapData = new BitmapData(100,100);
			
			_countPart = pixelBitmap.width * pixelBitmap.height;
			
			_partVector = new Vector.<Object>();
			var particle:Object;
			var pixelX:int=0;
			var pixelY:int = 0;
			
			for (var i:int = 0; i < _countPart; i++) {
				
				particle = new Object();
				particle.x		= STARTX + Math.random() * START_WIDTH;
				particle.y		= STARTY;
				particle.ox		= pixelX + 200;
				particle.oy		= pixelY + 200;
				particle.sy		= Math.random() * 30 + 6;
				particle.sx		= 0;
				particle.c		= pixelBitmap.getPixel(pixelX, pixelY);
			
				_partVector[i] = particle;
				
				
				pixelX++;
				if (pixelX % pixelBitmap.width == 0) {
					pixelX = 0;
					pixelY++;
				}
			}
			trace(Math.sqrt(10000));

			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			

		}
		
		private function onMouseInteraction(e:MouseEvent):void 
		{
			var type:String = e.type;
			switch(type) {
				case MouseEvent.MOUSE_OVER:
				_isOver = true;
				break;
				case MouseEvent.MOUSE_OUT:
				_isOver = false;
				break;
				case MouseEvent.CLICK:
				if (_animationType == "fall") {
					_animationType = "logo";
					for (var i:int = 0; i < _countPart; i++) {
						var particle:Object = _partVector[i] as Object;
						particle.sx = (particle.ox-particle.x)/30;
						particle.sy = (particle.oy-particle.y)/30;
					}
					_rewindCount = 29;
				} else {
					_animationType = "fall";
				}
				
				
				break;
			}
		}
		
		
		
		private function onEnterFrame(e:Event):void 
		{
			var mx:int = mouseX;
			var my:int = mouseY;
			
			
			
			var tmpBitmapData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFFFFFF);
			_sceneBmpData.lock();
			
			
			_sceneBmpData.applyFilter(_sceneBmpData, _sceneBmpData.rect, new Point(0, 0), _blur);
			
			_sceneBmpData.draw(_cache, null, null, BlendMode.NORMAL);
			
			
			var particle:Object;
			var px:Number;
			var py:Number;
			var ox:Number;
			var oy:Number;
			var sy:Number;
			var sx:Number;
			var c:uint;
			
			if (_rewindCount < 0) _animationType = "fall";
			
			for (var i:int = 0; i < _countPart; i++) {
				particle = _partVector[i] as Object;				px = particle.x;
				py = particle.y;
				ox = particle.ox;
				oy = particle.oy;
				sy = particle.sy;
				sx = particle.sx;
				c = particle.c;
				
				if (_animationType=="fall") {
					
					if (_isOver) {
						var dx:int = mx-px;
						var dy:int = my-py;
						var dist:int = Math.sqrt(dx*dx + dy*dy);
						if (dist<30) {
							px += (px - mx) < 0? -1:1;//= (px - mx) * 10;
							sx = (px - mx) / 3 + Math.random()*2;
							sy += Math.random()*.5+.5;
							py += dist/5;
						} else {
							px+=Math.random()*2-1;
						}
					}
					
					//px+=Math.random()*2-1;
					if (int(sx) > 0) {
						sx--;
					} else if (int(sx) < 0) {
						sx++;
					} else {
						sx = 0;
					}
					
					px += sx;
					
					
					
					py += sy;
					
					if (px < 0) px = _sw + px;
					if (px > _sw) px = px -_sw;
					
					
					
				
				
				
					
					
					if (py > _sh+50) {
						px = STARTX + Math.random() * START_WIDTH;
						px += Math.random() * 10 - 5;
						py = STARTY;// -5;
						sy = Math.random() * 30 + 6;
						sx = 0;
						
					}
				
				} else {
					px += sx;
					py += sy;
					if (_rewindCount == 0) {
						sx = 0;
						sy = 0;
						px = ox;
						py = oy;
					}
				}
				
				
				
				
				_sceneBmpData.setPixel(px, py, c);
				
				particle.x = px;
				particle.y = py;
				particle.sy = sy;
				particle.sx = sx;
				particle.c = c;
			}
			
			
			_sceneBmpData.applyFilter(_sceneBmpData, _sceneBmpData.rect, new Point(0, 0), _blur);
			
			_sceneBmpData.draw(tmpBitmapData, null, null, BlendMode.ADD);
			var storeedBitmapData:BitmapData = _sceneBmpData.clone();
			
			
			_sceneBmpData.draw(storeedBitmapData, null, null, BlendMode.ADD);
			_sceneBmpData.unlock();
			
			
		}
    }
}