[AS3.0] StarLightクラスだ!

StarlingのParticleSystemみたいなのをAS3.0で! :boy:

starLight31

:caution: 要 Flash Player 9 以上

This movie requires Flash Player 9.0.0

Main.as
package {

  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.geom.Rectangle;
  import flash.geom.Point;

  import effects.StarLight;

  [SWF(backgroundColor="#000000", width="600", height="400", frameRate="30")]

  public class Main extends Sprite {
    // プロパティ
    private var light:StarLight;
    private var _point:Point = new Point(300, 200);

    // コンストラクタ
    public function Main() {
      init();
    }

    // メソッド
    private function init():void {
      light = new StarLight(new Rectangle(0, 0, 600, 400));
      addChild(light);
      light.start();
      stage.addEventListener(MouseEvent.MOUSE_MOVE, update, false, 0, true);
    }
    private function update(evt:MouseEvent):void {
      var point:Point = new Point(mouseX, mouseY);
      if (distance(point)) {
        light.setup(point);
      }
    }
    private function distance(point:Point):Boolean {
      var d:Number = Point.distance(point, _point);
      _point = point;
      if (d > 4) {
        return true;
      } else {
        return false;
      }
    }

  }

}

StarLight (effects.StarLight.as)
package effects {

  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.geom.Rectangle;
  import flash.display.BitmapData;
  import flash.display.Bitmap;
  import flash.geom.ColorTransform;
  import flash.geom.Point;
  import flash.filters.BlurFilter;

  import effects.StarParticle;

  public class StarLight extends Sprite {
    // プロパティ
    private var rect:Rectangle;
    private var particles:Array;
    private var bitmapData:BitmapData;
    private var bitmap:Bitmap;
    private var container:Sprite;
    private static var colorTrans:ColorTransform;
    private static var point:Point = new Point();
    private static var blur:BlurFilter;

    // コンストラクタ
    public function StarLight(r:Rectangle) {
      rect = r;
      init();
    }

    // メソッド
    private function init():void {
      particles = new Array();
      bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
      bitmap = new Bitmap(bitmapData);
      addChild(bitmap);
      container = new Sprite();
      addChild(container);
      colorTrans = new ColorTransform(1, 1, 1, 0.4, 0, 0, 0, 0);
      blur = new BlurFilter(16, 16, 4);
    }
    public function start():void {
      addEventListener(Event.ENTER_FRAME, update, false, 0, true);
    }
    public function stop():void {
      removeEventListener(Event.ENTER_FRAME, update);
    }
    public function setup(pos:Object):void {
      var particle:StarParticle = new StarParticle(pos.x, pos.y);
      container.addChild(particle);
      particles.push(particle);
    }
    private function update(evt:Event):void {
      for (var n:uint = 0; n < particles.length; n++) {
        var particle:StarParticle = particles[n];
        particle.update();
        if (particle.life < 0 || particle.x < 0 || particle.x >rect.width || particle.y < 0 || particle.y > rect.height) {
          container.removeChild(particle);
          particles.splice(n, 1);
          particle = null;

        }
      }
      draw();
    }
    private function draw():void {
      bitmapData.lock();
      bitmapData.draw(container, null, colorTrans);
      bitmapData.applyFilter(bitmapData, rect, point, blur);
      bitmapData.unlock();
    }

  }

}

StarParticle (effects.StarParticle.as)
package effects {

  import flash.display.Sprite;
  import flash.geom.ColorTransform;
  import flash.display.BlendMode;

  import frocessing.color.ColorHSV;

  public class StarParticle extends Sprite {
    // プロパティ
    private var time:Number = 0;
    private static var max:uint = 250;
    private static var unit:Number = 1/max;
    public var life:Number = 1;
    private var px:Number = 0;
    private var py:Number = 0;
    private var angle:Number = 0;
    private var vx:Number = 0;
    private var vy:Number = 0;
    private static var speed:Number = 250;
    private static var radian:Number = Math.PI/180;
    private static var acceleration:Number = -400;
    private static var tacceleration:Number = -120;
    private var _scale:Number = 1;
    private var color:ColorHSV;
    private var colorTrans:ColorTransform;

    // コンストラクタ
    public function StarParticle(sx:Number, sy:Number) {
      px = sx;
      py = sy;
      draw();
      init();
    }

    // メソッド
    private function init():void {
      angle = Math.random()*360;
      vx = speed*Math.cos(angle*radian);
      vy = speed*Math.sin(angle*radian);
      x = px + vx*0.02;
      y = py + vy*0.02;
      color = new ColorHSV(- Math.random()*360, 0.5);
      colorTrans = new ColorTransform();
      colorTrans.color = color.value;
      transform.colorTransform = colorTrans;
      blendMode = BlendMode.ADD;
    }
    public function update():void {
      if (life < 0) return;
      time += unit;
      life = 1 – time*8;
      var dx:Number = x – px;
      var dy:Number = y – py;
      var distance:Number = Math.sqrt(dx*dx + dy*dy);
      if (distance < 0.01) distance = 0.01;
      var rx:Number = dx/distance;
      var ry:Number = dy/distance;
      var tx:Number = rx;
      var ty:Number = ry;
      rx *= acceleration;
      ry *= acceleration;
      var _tx:Number = tx;
      tx = – ty*tacceleration;
      ty = _tx*tacceleration;
      vx += (rx + tx)*time;
      vy += (ry + ty)*time;
      x += vx*time;
      y += vy*time;
      scale = life;
      rotation = 360*time;
    }
    private function draw():void {
      var star:Sprite = new Star();
      addChild(star);
    }
    private function get scale():Number {
      return _scale;
    }
    private function set scale(value:Number):void {
      _scale = value;
      scaleX = scaleY = _scale;
    }

  }

}

ライブラリに、星型のムービークリップ(リンケージ識別子: Star)を用意。