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

重力マウス(プチ軽量化:10万パーティクル)

リンクリストにしてみたけどそんなに速くない??
_bmd.fillRect()を_bmd.setPixel()に変更。
sin(),cos(),atan2(),sqrt()を排除。
Add final class / mouseEnalbled = false by clockmaker
/**
 * Copyright clockmaker ( http://wonderfl.net/user/clockmaker )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/d580
 */

// 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 clockmaker
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.ColorTransform;
    import flash.geom.Rectangle;
    import net.hires.debug.Stats;

    [SWF(frameRate='60', width='465', height='465', backgroundColor='0x0')]

    public class Main extends Sprite
    {
        private const MAX_NUM:int = 100000;
        private var _bmp:Bitmap;
        private var _bmd:BitmapData;
        private var _bmdRect:Rectangle;
        private var _colorTransform:ColorTransform = new ColorTransform(0.6, 0.7, 0.96, 1.0);
        private var _first:Particle;
        
        public function Main () {
            if(stage) init(null);
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(evt:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            var old:Particle;

            _bmd = new BitmapData(465, 465, false, 0x000000);
            _bmp = new Bitmap(_bmd);
            addChild(_bmp);
            this._bmdRect = _bmd.rect;

            for (var i:int = 0; i < MAX_NUM; i++) {
                var n:Particle = new Particle(
                    Math.random() * 465 >> 0,
                    Math.random() * 465 >> 0
                );
                //リンクリスト
                if (_first == null) {
                    old = _first = n;
                } else {
                    old.next = n;
                    old = n;
                }
            }
            
            addChild(new Stats());
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }

        private function enterFrameHandler(event:Event):void {
            var tempMouseX:Number = mouseX;
            var tempMouseY:Number = mouseY;
            var n:Particle = _first;
            
            _bmd.lock();
            do {
                var diffX:Number = tempMouseX - n.x;
                var diffY:Number = tempMouseY - n.y;
                var acc:Number = 200 / (diffX * diffX + diffY * diffY);
                var accX:Number = acc * diffX;
                var accY:Number = acc * diffY;
                
                n.vx += accX;
                n.vy += accY;
                
                n.x += n.vx;
                n.y += n.vy;
                
                n.vx *= 0.96;
                n.vy *= 0.96;
          
                if (n.x > 465)
                    n.x -= 465;
                else if (n.x < 0)
                    n.x += 465;
                if (n.y > 465)
                    n.y -= 465;
                else if (n.y < 0)
                    n.y += 465;
                
                _bmd.setPixel(n.x >> 0, n.y >> 0, 0xffffff); 
            }
            while (n = n.next);
            
            _bmd.colorTransform(_bmdRect, _colorTransform);
            _bmd.unlock();
        }
    }
}

/** パーティクルクラス */
class Particle {
    /** コンストラクタ */
    public function Particle(x:Number, y:Number){
        this.x = x;
        this.y = y;
    }
    public var x:Number = 0; // X座標
    public var y:Number = 0; // X座標
    public var vx:Number = 0; // X方向の速さ
    public var vy:Number = 0; // Y方向の速さ
    public var next:Particle; // LinkedListの参照
}