Metaball by BitmapData.threshold
しきい値による擬似メタボール
package{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.filters.*;
[SWF(backgroundColor="0x000000", frameRate="30")]
//しきい値による擬似メタボール
public class MetaballThreshold extends Sprite{
private var ballMax:uint = 10;//ボールの数
private var ballContainer:Sprite;//ボール表示用コンテナ
private var bmp:Bitmap;//ここに描画してく
private var bmpData:BitmapData;//bmp用
private var tmpBmpData:BitmapData;//threshold処理用
public function MetaballThreshold(){
this.addEventListener( Event.ADDED_TO_STAGE, onAddedHandler);//
}
private function onAddedHandler( e:Event ):void{
this.removeEventListener( Event.ADDED_TO_STAGE, onAddedHandler);//
ballContainer = new Sprite();
ballContainer.visible = false;
addChild( ballContainer );
for(var i:uint=0; i<ballMax; i++){
var ball:Ball = new Ball();
ballContainer.addChild(ball);
}
bmpData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFF000000);
tmpBmpData = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0xFF000000);
bmp = new Bitmap( bmpData );
addChild( bmp );
this.addEventListener( Event.ENTER_FRAME, onEnterFrame);//
}
private function onEnterFrame( e:Event ):void{
tmpBmpData.fillRect( tmpBmpData.rect, 0xFF000000);
tmpBmpData.draw( ballContainer );
bmpData.fillRect( tmpBmpData.rect, 0xFF000000);
//tmpBmpDataの0x808080(50%)より白い箇所を0xD9FF66に置換してbmpDataにdrawする
bmpData.threshold( tmpBmpData, bmpData.rect, new Point( 0, 0), ">=", 0x00808080, 0xFFD9FF66, 0x00FFFFFF, false);
}
}
}
import flash.display.*;
import flash.events.*;
import flash.filters.*;
class Ball extends Sprite{
private var _ball:Sprite;
private var ballSize:Number = 70;//ボールの半径
private var speedX:Number = (Math.random()*6 - 3)*(Math.floor(Math.random()*2)*2 - 1);//移動スピードX
private var speedY:Number = (Math.random()*6 - 3)*(Math.floor(Math.random()*2)*2 - 1);//移動スピードY
private var blurFilter:BlurFilter = new BlurFilter( 32, 32, 1);//ブラー(ちょっと多めに)
public function Ball(){
this.addEventListener( Event.ADDED_TO_STAGE, onAdded);//
}
private function onAdded( e:Event ):void{
this.removeEventListener( Event.ADDED_TO_STAGE, onAdded);//
_ball = new Sprite();
_ball.graphics.beginFill( 0xFFFFFF, 1);
_ball.graphics.drawCircle( 0, 0, ballSize);
_ball.graphics.endFill();
_ball.x = stage.stageWidth/2;
_ball.y = stage.stageHeight/2;
_ball.filters = [blurFilter];
addChild(_ball);
this.addEventListener( Event.ENTER_FRAME, moveBall);//
}
private function moveBall( e:Event ):void{
_ball.x += speedX;
_ball.y += speedY;
if( _ball.x > stage.stageWidth - ballSize ){
_ball.x = stage.stageWidth - ballSize;
speedX *= -1;
}else if( _ball.x < ballSize ){
_ball.x = ballSize;
speedX *= -1;
}
if( _ball.y > stage.stageHeight - ballSize ){
_ball.y = stage.stageHeight - ballSize;
speedY *= -1;
}else if( _ball.y < ballSize ){
_ball.y = ballSize;
speedY *= -1;
}
}
}