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

カメラとバウンドボール

これも処理重め
色が違う所でボールがバウンドします。
バックがベタ一色の壁だと好都合
Get Adobe Flash player
by cda244 15 Feb 2009
    Embed
/*
これも処理重め
色が違う所でボールがバウンドします。
バックがベタ一色の壁だと好都合
*/

package{
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.media.Video;
	import flash.media.Camera;
	
	 [SWF(width="320", height="240", backgroundColor="0xFFFFFF", frameRate="30")]  
	
	public class Main extends MovieClip
	{
		private const W:uint=320;
		private const H:uint=240;
		private const BALL_NUM:uint = 30;
		private var crntBallNum:uint=0;
		
		private var camera:Camera;
		private var video:Video;
		
		private var bmpMC:MovieClip;
		private var bmp:Bitmap;

		private var bmpDat:BitmapData;
		
		private var pxArray:Array;

		
		public function Main():void
		{
			camera = Camera.getCamera();
			
			if ( camera != null ) {
				camera.setMode(W, H, 30);
				video = new Video( W, H );
				video.attachCamera( camera );
				video.visible=false;
				
				bmpDat = new BitmapData(W, H);
				bmp = new Bitmap( bmpDat );

				bmpMC=new MovieClip();
				bmpMC.addChild( bmp );
				addChild(bmpMC);
				
				pxArray = new Array(W);
				for(var i:uint = 0; i<=W; i++){
					pxArray[i] = new Array(H);
					for(var j:uint = 0; j<=H; j++){
						pxArray[i][j] = 0;
					}
				}
				
				addEventListener( Event.ENTER_FRAME , capVideo );
			}
		}
		
		
		private function capVideo( e:Event ):void{
			bmpDat.draw(video);
			
			var diffCount:uint=0;
			
			for(var i:uint = 0; i<=W; i++){
				for(var j:uint = 0; j<=H; j++){
					var color:uint = bmpDat.getPixel(i, j);
					var diff:uint = Math.abs(color - pxArray[i][j])/65535;
					
					if(diff>110){	diffCount++;	}
					
					pxArray[i][j] = color;
				}
			}
			if(diffCount > 300 && crntBallNum < BALL_NUM){
				var ball:Ball=new Ball();
				addChild(ball);
				//違いが大きいピクセルの数/200を半径として渡す。1.5px以上
				ball.init( diffCount*0.005 );
				crntBallNum++;
			}
		}
		
		public function getColor(prmX:uint, prmY:uint):uint{	return bmpDat.getPixel(prmX, prmY);	}

		
		public function removeBall():void{	crntBallNum --;	}
		
	}
	
	
}
	
	import flash.geom.Point;
	import flash.display.MovieClip;
	import flash.events.Event;

	class Ball extends MovieClip
	{
		
		static const W:uint=320;
		static const H:uint=240;
		private var colorArray:Array=[0, 0];

		private var t:uint=0;
		private var a:Number=5;
		private var v0:int=0;
		private var v:Point;
		
		private const MAX_BOUND_NUM:uint=10; 
		private var crntBoundNum:uint=0;
		
		public function Ball(){
		}
		
		public function init(prmR:Number):void{
			x = 1+Math.random()*(W-1);
			y = -(50+height);
			v = new Point(0, 0);
			
			graphics.beginFill( 0xFFFFFF * Math.random() );
			graphics.drawCircle(0, 0, Math.min(prmR, 20) );
			graphics.endFill( );
			
			addEventListener(Event.ENTER_FRAME, fall);
		}
		
		private function fall(evt:Event):void{
			
			if(x > width && y < W-width && y > height && y < H-height){
				var newColor = (parent as MovieClip).getColor(x, y);
				
				var newR = ( newColor & 0xff0000 ) >> 16;
				var newG = ( newColor & 0xff00 ) >> 8;
				var newB = ( newColor & 0xff );
				
				var oldR = ( colorArray[0] & 0xff0000 ) >> 16;
				var oldG = ( colorArray[0] & 0xff00 ) >> 8;
				var oldB = ( colorArray[0] & 0xff );
				
				var diff:uint = (newR-oldR)*(newR-oldR) + (newG-oldG)*(newG-oldG) + (newB-oldB)*(newB-oldB);
				
				//バウンド
				if(diff > 15000 && crntBoundNum < MAX_BOUND_NUM){
					v0 = -20;
					v.x = -2 + Math.random()*4;
					t = 0;
					
					crntBoundNum ++;
				}
				
				colorArray[0] = colorArray[1];
				colorArray[1] = newColor;
			}
			else if(x<-width || x>W+width || y > H+height){
				//画面外
				removeEventListener(Event.ENTER_FRAME, fall);
				(parent as MovieClip).removeBall();
				(parent as MovieClip).removeChild(this);
			}

			v.y = v0 + a*t;
			x += v.x;
			y += v.y;
			
			t ++;
			
		}
		
	}