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

重力マウス(もっと軽量化:20万パーティクル)

setVectorとgetVectorを使うとだいぶ軽いみたいです。
Get Adobe Flash player
by shohei909 10 Dec 2010
/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/8XnB
 */

// forked from clockmaker's 重力マウス(プチ軽量化:10万パーティクル)
// forked from coppieee's 重力マウス(さらに軽量化してみた)
// forked from paq's forked from: 重力マウス(ちょっぴり軽量化してみた)
// forked from fumix's 重力マウス(リンクリストにしてみた)
// forked from undo's 重力マウス
// リンクリストにしてみたけどそんなに速くない??
//_bmd.fillRect()を_bmd.setPixel()に変更。
//sin(),cos(),atan2(),sqrt()を排除。
// Add final class / mouseEnalbled = false by clockmake
// Use setVector & getVector by shohei909
package
{
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.geom.ColorTransform;
    import flash.geom.Rectangle;
    import net.hires.debug.Stats;

    [SWF(frameRate='60', width='465', height='465')]
    public class ParticleTest1 extends Sprite
    {
        private const _bmd:BitmapData = new BitmapData(465, 465, false, 0);
        private const _colorTransform:ColorTransform = new ColorTransform(0.5, 0.5, 0.75, 1.0);
        private var _first:Node;
        

        function ParticleTest1()
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            const _maxNum:uint = 200000;
            var old:Node;
            for (var i:uint = 0; i < _maxNum; i++)
            {
                var n:Node = new Node();
                n.pos_x = Math.random()*10 + (465 >> 1);
                n.pos_y = Math.random()*10 + (465 >> 1);
                //リンクリスト
                if (_first) {
                    old.next = n;
                    old = n;
                } else {
                    old = _first = n;
                }
            }
            
            with(stage){
                align = "topLeft";
                scaleMode = "noScale";
                quality = "low";
                frameRate = 60; 
                mouseChildren = false;
                tabChildren = false;
                addChild( new Bitmap(_bmd,"never",false) ).cacheAsBitmap = false;
                addChild( new Stats() ).cacheAsBitmap = false;
                removeChildAt(0);
            }
            this._bmd.lock();
            
            addEventListener(Event.EXIT_FRAME, frame);
        }

        private function frame(e:Event):void{ 
            const rect:Rectangle = _bmd.rect;
            const gx:Number = mouseX, gy:Number = mouseY, vec:Vector.<uint> = _bmd.getVector(rect);
            vec.fixed = true;
            var n:Node = _first;
            do{
                var p0:Number, p1:Number, p2:Number, p3:Number;
                const diff_x:Number = gx - ( p0 = n.pos_x += p2 = n.v_x );
                const diff_y:Number = gy - ( p1 = n.pos_y += p3 = n.v_y );
                const acc:Number = 256 / ( diff_x * diff_x + diff_y * diff_y + 1 );
                n.v_x = p2 * 0.875 + acc * diff_x;
                n.v_y = p3 * 0.875 + acc * diff_y;
                if( (p0>>>=0) < 465 && (p1>>>=0) < 465 ){ vec[(p0 + p1*465)>>>0] = 0xffffff }
            }while( (n = n.next) !== null );
            
            _bmd.setVector( rect, vec ); 
            _bmd.colorTransform( rect, this._colorTransform );
            _bmd.unlock();
            _bmd.lock();
        }
    }
}

final class Node extends Object
{
    public var v_x:Number = 0;
    public var v_y:Number = 0;
    public var pos_x:Number = 0;
    public var pos_y:Number = 0;
    public var next:Node = null;
}