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

BitmapData練習

BitmapDataの練習。パーティクルできれいな画をつくるの難しいです。
次回以降頑張りたいです。
Get Adobe Flash player
by sat 24 Feb 2010
package
{
	/*
	BitmapDataの練習。パーティクルできれいな画をつくるの難しいです。
	次回以降頑張りたいです。
	*/
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	[SWF(width=465,height=465,frameRate=24,backgroundColor=0)]
	
	public class Water extends Sprite
	{
		public var water:Sprite;
		public var originalBitmapData:BitmapData;
		public var waters:Array;
		
		public function Water()
		{
			//particleを入れる配列
			waters = new Array();
			
			//元となる図形だよ
			water = new Sprite();
			water.graphics.lineStyle(1,0);
			water.graphics.beginFill(0);
			water.graphics.drawCircle(220,400,50);
			water.graphics.endFill();
			
			//canvasになるBitmapDataだよ
			originalBitmapData = new BitmapData(465,465,false,0x00cbff);
			originalBitmapData.draw(water);
			addChild( new Bitmap(originalBitmapData) );
			
			setWater();
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, breakWater);
		}
		
		
		//元の図形をチェックして、パーティクルにしてくよ
		private function setWater():void
		{
			var cl:int = 0;
			//全ピクセルをチェックしていく
			for(var i:Number = 0; i < 465; i++)
			{
				for(var j:Number = 0; j < 465; j++)
				{
					cl = originalBitmapData.getPixel(i,j);					
					//色が黒のピクセルだけパーティクルを発生
					if(cl == 0)
					{
						var wp:WaterParticle = new WaterParticle(i,j,0x00cbff);
						//配列につっこんでいく
						waters.push(wp);
					}
				}
			}		
		}
		
		//はじけさせるほう
		public function breakWater(e:MouseEvent):void
		{
			removeEventListener(Event.ENTER_FRAME, moveWater2);
			addEventListener(Event.ENTER_FRAME, moveWater1);
				
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, breakWater);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, reverseWater);
		}
		//戻すほう
		public function reverseWater(e:MouseEvent):void
		{
			removeEventListener(Event.ENTER_FRAME, moveWater1);
			addEventListener(Event.ENTER_FRAME, moveWater2);
			
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, reverseWater);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, breakWater);
		}
		
		
		//はじけるメソッド
		public function moveWater1(e:Event):void
		{
			var tempWaters:uint = waters.length;
			originalBitmapData.lock();
			originalBitmapData.fillRect(originalBitmapData.rect, 0x000000);
			
			
			for(var k:Number = 0; k < tempWaters; k++)
			{
				waters[k].x += Math.random() * mouseX / 2 - mouseX * 0.25;
				waters[k].y -= Math.random() * mouseY / 3;
				
				if(waters[k].y < 0)
				{
					waters[k].y *= -1;
				}
				waters[k].wColor += 1;
				
				originalBitmapData.setPixel(waters[k].x,waters[k].y,waters[k].wColor);
				
			}
			
			originalBitmapData.unlock();
		}
		
		//戻るメソッド
		public function moveWater2(e:Event):void
		{
			var tempWaters:uint = waters.length;
			originalBitmapData.lock();
			originalBitmapData.fillRect(originalBitmapData.rect, 0x000000);
			
			for(var l:Number = 0; l < tempWaters; l++)
			{
				waters[l].x += (waters[l].startX - waters[l].x) / 3;
				waters[l].y += (waters[l].startY - waters[l].y) / 3;
				
				//スナップ処理
				if(waters[l].x - waters[l].startX < 0.3 && waters[l].x - waters[l].startX > -0.3)
				{
					waters[l].x = waters[l].startX;
					waters[l].y = waters[l].startY;
					
				}
				
				originalBitmapData.setPixel(waters[l].x,waters[l].y,waters[l].wColor);
			}
			originalBitmapData.unlock();	
			
		}
		
	}
}

//WaterParticleクラス
import flash.display.Sprite;

class WaterParticle extends Sprite
{
	public var startX:Number;
	public var startY:Number;
	public var startC:int;
	public var wColor:int;
		
	public function WaterParticle(a:Number, b:Number, c:int)
	{
		startX = a;
		startY = b;
		startC = c;
		wColor = c;
		x = startX;
		y = startY;
			
	}
}