2011年4月27日 星期三

Bitmap 粒子(一)

package
{
 import flash.display.Bitmap;
 import flash.display.BitmapData;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.filters.BitmapFilterQuality;
 import flash.filters.BlurFilter;
 import flash.geom.Point;
 [SWF(width="1024", height="768", frameRate="30", backgroundColor="#000000", pageTitle="粒子測試")]
 public class particale extends Sprite
 {
  private var _bitmapData:BitmapData;
  private var _photosphereMap:Bitmap;
  private var _sprite:Sprite = new Sprite();   
  //活動場景大小
  private var _mapWidth:Number = 400;
  private var _mapHeight:Number = 400;
  //顆粒大小
  private var _particaleSize:Number = 10;  
  //位置與量
  private var _position:Point;
  private var _vector:Point;
  //濾鏡強度
  private var blurX:Number = 1.5;
  private var blurY:Number = 1.5;

  public function particale()
  {
   super();
   init();
  }
  private function init():void
  {
   initPhotosphereMap();
   deployPhotosphere();
   initPostiton();
   initVector();
   onStart();
  }
  private function initPhotosphereMap():void
  {
   _bitmapData = new BitmapData(_mapWidth,_mapHeight,true,0);
   _photosphereMap = new Bitmap(_bitmapData);
  }

  private function deployPhotosphere():void
  {
   this.addChild(_photosphereMap);
  }

  private function initPostiton():void
  {
   _position = new Point(centerX , centerY);
  }
  private function get centerX():Number
  {
   return _mapWidth >> 2;
  }
  private function get centerY():Number
  {
   return _mapHeight >> 2;
  }

  private function initVector():void
  {
   _vector = new Point();
  }

  private function onStart():void
  {
   stage.addEventListener(Event.ENTER_FRAME,loop);
  }

  private function loop(e:Event):void
  {
   drawParticale();
  }

  private function drawParticale():void
  {
   _sprite.graphics.clear();
   _value++;
   _sprite.graphics.lineStyle(_particaleSize,color,1);
   _sprite.graphics.moveTo(_position.x,_position.y);
   changedVector();
   setNextPosition();
   checkPosition();
   _sprite.graphics.lineTo(_position.x ,_position.y);
   _bitmapData.draw(_sprite);
   _bitmapData.applyFilter(_bitmapData,_bitmapData.rect,new Point(),new BlurFilter(blurX,blurY,BitmapFilterQuality.HIGH));
  }

  //更新量
  private function changedVector():void
 {
  randomVectorX();
  randomVectorY();
 }
 private function randomVectorX():Number
 {
  return _vector.x += 3*(Math.random()-0.5);
 }
 private function randomVectorY():Number
 {
  return _vector.y += 3*(Math.random()-0.5);
 }
 //更新座標點
 private function setNextPosition():void
 {
  _position.x = getNextX(_position.x , _vector.x);
  _position.y = getNextY(_position.y , _vector.y);
 }
 private function getNextX(nowX:Number , vectorX:Number):Number
 {
  return nowX + vectorX;
 }
 private function getNextY(nowY:Number , vectorY:Number):Number
 {
  return nowY + vectorY;
 }
 //檢查點,防止超出範圍
 private function checkPosition():void
 {
  //check x 軸
  if (_position.x < 0) {
   _position.x = 0; _vector.x *= -0.5
  }
  if (_position.x > _mapWidth) {
   _position.x = _mapWidth;
   _vector.x *= -0.5;
  }
  //check y 軸
  if (_position.y < 0){
    _position.y = 0; _vector.y *= -0.5;
  }
   if (_position.y > _mapHeight){
   _position.y = _mapHeight;
   _vector.y *= -0.5;
  }
 }

 private function get color():uint
 {
  return Math.round(Math.random()* 0xFFFFFF);
 }
 }
}
//畫面

沒有留言:

張貼留言