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

血しぶき高速化

Get Adobe Flash player
by 3f5 29 Aug 2011
/**
 * Copyright 3f5 ( http://wonderfl.net/user/3f5 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/x6sw
 */

// forked from 3f5's 血しぶき
// forked from yuitest's forked from: ビックバンです
// forked from 3f5's ビックバンです
package {
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.display.Sprite;
    import net.hires.debug.Stats;
    [SWF(frameRate="60", width="465", height="465")]
    public class Test extends Sprite {
        private const list:Vector.<Blood> = new Vector.<Blood>();
        private var bitmap:BitmapData;
        private const rect:Rectangle = new Rectangle();
        private var bitmapRect:Rectangle;
        private var debug:TextField = new TextField;
        private var timer:Number = -1;

        public function Test() {
            stage.quality = "low";
            stage.mouseChildren = false;

            bitmap = new BitmapData(465, 465, false, 0xFFFFFF);
            bitmapRect = bitmap.rect;
            
            debug.text = 'クリックで血しぶき'

            stage.addEventListener(MouseEvent.MOUSE_DOWN, function (event:MouseEvent):void {
                if (timer == -1) {
                    timer = new Date().getTime();
                    debug.text = '計測中...';
                }

                var x:int = stage.mouseX;
                var y:int = stage.mouseY;

                for (var i:uint = 0; i < 100; i++) {
                    push(x, y, 10,0);
                }
            });
            addChild(new Bitmap(bitmap));
            addChild(debug);
            //addChild(new Stats());
            addEventListener(Event.ENTER_FRAME, update);
        }
        private function push(x:Number, y:Number, size:int, gravity:Number):void {
            list[list.length] = new Blood(x, y, size, gravity, rand(rand(-5, -1), rand(1, 5)), rand(rand(-5, -1), rand(1, 5)));
        }

        private function update(event:Event):void {
            //event.stopPropagation();
            var blood:Blood, i:uint;
            bitmap.lock();
            bitmap.fillRect(bitmapRect, 0xffffff);
            for (i = 0; i < list.length; i++) {
                blood = list[i];
                
                if (blood.y > 465) {
                    list[i] = null;
                    blood = null;
                    list.splice(i, 1);
                    i--;
                    continue;
                }
                
                if (rand(1, 15) === 1 && blood.size - 3 >= 1) {
                    push(blood.x, blood.y, blood.size - 3, blood.gravity);
                    push(blood.x, blood.y, blood.size - 3, blood.gravity);
                    push(blood.x, blood.y, blood.size - 3, blood.gravity);
                    push(blood.x, blood.y, blood.size - 3, blood.gravity);
                    push(blood.x, blood.y, blood.size - 3, blood.gravity);
                    list[i] = null;
                    blood = null;
                    list.splice(i, 1);
                    i--;
                    continue;
                }


                blood.aX *= 0.99;
                blood.gravity += 0.002;
                blood.aY = blood.aY * 0.99 + blood.gravity;
                blood.x += blood.aX;
                blood.y += blood.aY;
                
                rect.width = blood.size;
                rect.height = blood.size;
                rect.x = blood.x;
                rect.y = blood.y; 
                bitmap.fillRect(rect, 0xFF0000)
            }
            bitmap.unlock();
            if (list.length === 0 && timer !== -1) {
                debug.text = String((new Date().getTime()) - timer) + 'ms';
                timer = -1;
            }

        }

        
        private function rand(min:int, max:int):int {
            var num:Number = Math.random() * (max - min + 1) + min;
            return (num >= 0) ? (~~num) : ~~(num - 1);
        }

    }
}

final class Blood
{
    public var aY:Number;
    public var aX:Number;
    public var size:int;
    public var gravity:Number;
    public var x:Number;
    public var y:Number;
 
    public function Blood(_x:Number, _y:Number, _size:int,_gravity:Number, _aX:Number, _aY:Number) {
        x = _x;
        y = _y;
        size = _size;
        gravity = _gravity;
        aX = _aX;
        aY = _aY;
    }
}