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

forked from: Particle Sample Part2

大量のパーティクルを発生させてみた
// forked from cellfusion's Particle Sample Part2
// forked from cellfusion's Particle Sample
// write as3 code here..
// 大量のパーティクルを発生させてみた
package
{
    import flash.display.Sprite;
    import flash.display.StageQuality;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import net.hires.debug.Stats;

    [SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")]  
	
    public class ParticleSample extends Sprite
    {
        
        private var _particles:Array = [];
        private var _emitter:Emitter;
        // 1フレーム間に発生させる Particle 数
        private const PARTICLE_NUM:uint = 40;

        public function ParticleSample()
        {
            stage.quality = StageQuality.LOW;
            setup();
            addChild(new Stats());
        }
        
        private function setup():void
        {
            _emitter = new Emitter();

            for (var i:uint = 0; i < 10000; i++) {
                _particles.push(new Particle());
            }

            addEventListener(Event.ENTER_FRAME, draw);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown); 
        }
        
        private function draw(event:Event):void
        {
            _emitter.update(root.mouseX, root.mouseY);

            for each (var p:Particle in _particles) {
                if (!p.destroy) {
                    // 重力
                    //p.vy -= 0.98;
                    // 風
                    //p.vx += 0.5;
                    
                    // 床
                    /*if (p.y >= 465 || p.y <= 0) {
                        p.vy *= -0.9;
                    }*/

                    /*if (p.x >= 465 || p.x <= 0) {
                        p.vx *= -0.9;
                    }*/

                    p.update();
                }
            }
        }

        private function mouseDown(event:MouseEvent):void
        {
            addEventListener(Event.ENTER_FRAME, createParticle);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
        }

        private function mouseUp(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
            removeEventListener(Event.ENTER_FRAME, createParticle);
        }         

        private function createParticle(event:Event):void
        {
            var count:uint = 0;
            for each (var p:Particle in _particles) {
                // 停止している Particle を探す
                if (p.destroy) {
                    p.x = _emitter.x;
                    p.y = _emitter.y;
                    p.init();
                    addChild(p);
                    count++;
                }
                if (count > PARTICLE_NUM) break;
            }
        }
    }
}


import flash.display.Sprite;
import flash.display.GradientType;

class Emitter
{
    public var x:Number = 0;
    public var y:Number = 0;
    public var vx:Number = 0;
    public var vy:Number = 0;

    public function Emitter()
    {       
    }

    public function update(targetX:Number, targetY:Number):void
    {
        var dx:Number = targetX - x;
        var dy:Number = targetY - y;
        var d:Number = Math.sqrt(dx*dx+dy*dy) * 0.2;
        var rad:Number = Math.atan2(dy, dx);

        d = Math.min(Math.max(-20, d), 20);

        vx += Math.cos(rad)*d;
        vy += Math.sin(rad)*d;

        vx *= 0.7;
        vy *= 0.7;

        x += vx;
        y += vy;
    }
}


import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.display.SpreadMethod;

class Particle extends Sprite
{
    public var vx:Number;
    public var vy:Number;
    public var life:Number;
    public var size:Number;

    private var _count:uint;
    private var _destroy:Boolean;

    public function Particle()
    {
        size = Math.random() * 10 + 5;
        
        var red:uint = Math.floor(Math.random()*100);
        var green:uint = Math.floor(Math.random()*100+100);
        var blue:uint = Math.floor(Math.random()*256);
        var color:Number = (red << 16) | (green << 8) | (blue);
        
        var fillType:String = GradientType.RADIAL;
        var colors:Array = [color , 0x000000];
        var alphas:Array = [100, 100];
        var ratios:Array = [0x00, 0xFF];
        var mat:Matrix = new Matrix();
        mat.createGradientBox(size * 2, size * 2, 0, -size, -size);
        var spreadMethod:String = SpreadMethod.PAD;

        graphics.clear();
        graphics.beginGradientFill(fillType, colors, alphas, ratios, mat, spreadMethod);
        graphics.drawCircle(0, 0, size);
        graphics.endFill();
        
        //filters = [new BlurFilter(8,8,2)];
        
        // 大量のオブジェクトを重ねるとおかしくなる
        blendMode = "add";

        _destroy = true;
    }

    public function init():void
    {
        vx = Math.random() * 50 - 25;
        vy = Math.random() * 50 - 25;
        life = Math.random() * 90 + 10;
        _count = 0;
        _destroy = false;
    }

    
    public function update():void
    {
        x += vx;
        y += vy;

        _count++;
        
        // 死亡フラグ
        if (life < _count) {
            _destroy = true;
            parent.removeChild(this);
        }
    }

    public function get destroy():Boolean
    {
        return _destroy;
    }
}