Metaball via BitmapData.threshold/Blur Value
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 = 2;
private var ballContainer:Sprite;
private var bmp:Bitmap;
private var bmpData:BitmapData;
private var tmpBmpData:BitmapData;
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 = 100;
private var speedX:Number = (Math.random()*6 - 3)*(Math.floor(Math.random()*2)*2 - 1);
private var speedY:Number = (Math.random()*6 - 3)*(Math.floor(Math.random()*2)*2 - 1);
private var blurFilter:BlurFilter = new BlurFilter( 64, 64, 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;
}
}
}