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: 踊るビルディング

とりあえずちょっとダンス。
Get Adobe Flash player
by sat 24 Feb 2010
// forked from sat's 踊るビルディング
package
{
	/*
	とりあえずちょっとダンス。
	*/
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.display.Graphics;
	
	[SWF(width=150,height=300,frameRate=24,backgroundColor=0xffffff)]
	
	import flash.utils.Timer;
	public class UkiBuilding extends Sprite
	{
		public var bldg:Sprite;
		public var canvas:BitmapData;
		public var bldgParts:Array;
		public var bmp:Bitmap;
		public var timer:Timer;
		
		public var vertices:Vector.<Number>;
		public var indices:Vector.<int>;
		public var uvtData:Vector.<Number>;
		public var choutens:Array;
		public var g:Graphics;
		public var sample:BitmapData = new BitmapData(100,150,false,0);
		
		//===窓の色情報===
		public var colors:Array;
		
		
		public function UkiBuilding()
		{
			init();
		}
		
		public function init():void
		{
			bldgParts = new Array();
			colors = new Array();
			colors.push(0xff0000,0xffcc33,0xff3399,0xcc00cc,0xccff33);
			
			//===drawTriangle用===
			vertices = new Vector.<Number>();
			indices = new Vector.<int>()
			uvtData = new Vector.<Number>()
			choutens = new Array();
			g = graphics;
			
			//====canvas===
			canvas = new BitmapData(150,300,false,0xffffff);
			
			//====まずはビルを描く======
			bldg = new Sprite();
			bldg.graphics.beginFill(0);
			bldg.graphics.drawRect(35,90,80,210);
			bldg.graphics.endFill();
			
			//====窓を描く======
			for(var i:uint = 0; i < 6; i++)
			{
				for(var j:uint = 0; j < 3; j++)
				{
					var windowColor:Number = colors[ Math.floor( Math.random()*colors.length ) ]
					bldg.graphics.beginFill(windowColor);
					bldg.graphics.drawRect(40 + j * 25, 95 + i * 25, 20, 20);
					bldg.graphics.endFill();
				}
				
			}
			
			canvas.draw(bldg);
			setBitmapData();
			
			//====踊り出す処理====
			stage.addEventListener(MouseEvent.MOUSE_DOWN, danceBldg);
			
			
			
		}
		
		//===元画像をパーティクルに変換=====
		private function setBitmapData():void
		{
			var cl:int = 0;
			
			//全ピクセルをチェックしていく
			for(var i:Number = 0; i < 150; i++)
			{
				for(var j:Number = 0; j < 300; j++)
				{
					cl = canvas.getPixel(i,j);					
					//色が白以外のピクセルだけパーティクルを発生
					if(cl != 16777215)
					{
						var parts:BldgParticle = new BldgParticle(i,j,cl);
						//配列につっこんでいく
						bldgParts.push(parts);
					}
				}
			}		
			
			//===いったんcanvasを真っ白に====
			canvas.fillRect(canvas.rect,0xffffff);
			
			//===パーティクルを作ってから、ビルを建てる====
			makeBuilding();
			
		}
		
		private function makeBuilding():void
		{
			addChild( bmp = new Bitmap(canvas) );
			addEventListener(Event.ENTER_FRAME, moveParts);
		}
		
		//===ビルが出来てく様子====
		private function moveParts(e:Event):void
		{				
			var tempParts:int = bldgParts.length;
			canvas.lock();
			canvas.fillRect(canvas.rect, 0xffffff);
			
			for(var k:int = 0; k < tempParts; k++)
			{
				bldgParts[k].y += (bldgParts[k].startY - bldgParts[k].y) / 5 * Math.random();
				bldgParts[k].x += (bldgParts[k].startX - bldgParts[k].x) / 5 * Math.random();
				
				//===スナップ処理===
				if(bldgParts[k].startY - bldgParts[k].y < 3)
				{
					bldgParts[k].y = bldgParts[k].startY;
					bldgParts[k].x = bldgParts[k].startX;
						
				}
				
				canvas.setPixel(bldgParts[k].x,bldgParts[k].y,bldgParts[k].startC);
			}
			canvas.unlock();
					
			
		}
		
		private function danceBldg(e:MouseEvent):void
		{
			removeEventListener(Event.ENTER_FRAME, moveParts);
			setDrawTriangle();
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, danceBldg);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, reset);
			
		}
		
		private function setDrawTriangle():void
		{
			//===まずは頂点を並べる===
			for(var i:uint = 0; i < 8; i++)
			{
				for(var j:uint = 0; j < 2; j++)
				{
					var chouten:BldgParticle = new BldgParticle(j * 150, i * 42,0);
					choutens.push(chouten);
					vertices.push(chouten.startX, chouten.startY);
				}
			}
			
			
			//===三角形の頂点情報===
			
			for(var l:uint = 0; l < 14; l++)
			{
				if(l%2 == 0)
				{
					indices.push(l, l + 1, l + 2);
					indices.push(l + 1, l + 2, l + 3);
				}
			}
			
			
			for (var m:Number=0; m<8; m++) 
			{
				for (var n:Number=0; n<2; n++) 
				{
					uvtData.push(n, m/7);
				}
			}
			
			g.clear();
			g.beginBitmapFill(canvas);
			g.drawTriangles(vertices, indices, uvtData);
			g.endFill();
			//===上にあるBitmapを隠しておく===
			bmp.visible = false;
			
			timer = new Timer(300);
			timer.addEventListener(TimerEvent.TIMER, moveImage);
			timer.start();
		}
		
		//===ビルのダンス===
		private function moveImage(e:TimerEvent):void
		{
			for(var i:uint = 0; i < vertices.length; i++)
			{
				if(i%2 ==0)
				{
					var tension:Number = (300 - choutens[i/2].startY) / 3;
					vertices[i] = choutens[i/2].startX - 15 +Math.random()*tension;
					
					g.clear();
					g.beginBitmapFill(canvas);
					g.drawTriangles(vertices, indices, uvtData);
					g.endFill();
				}
			}
		}
		
		//===リセット処理====
		private function reset(e:MouseEvent):void
		{
			removeEventListener(Event.ENTER_FRAME, moveImage);
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, reset);
			g.clear();
			init();
		}
					
			
		
		
	}
}


import flash.display.Sprite;
	
class BldgParticle extends Sprite
{
	public var startX:Number;
	public var startY:Number;
	public var startC:int;
		
	public function BldgParticle(a:Number, b:Number, c:int)
	{
		startX = a;
		startY = b;
		startC = c;
		x = startX - 40 + Math.random() * 80;
		y = 0;
			
	}
		
}