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

BitmapData pieces

press mouse to add force
/**
 * Copyright fcukie ( http://wonderfl.net/user/fcukie )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dI1h
 */

package  {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import net.hires.debug.Stats;

    [SWF(backgroundColor="#ffffff", width="465", height="465", frameRate="60")]
    public class Main extends Sprite {

        static public const BMD_WIDTH:uint = 465;
        static public const BMD_HEIGHT:uint = 465;

        static public const PIECE_WIDTH:uint = 10;
        static  public const PIECE_HEIGHT:uint = 10;
        
        private var offset_x:int;
        private var offset_y:int;        

        private var bmd:BitmapData;
        private var bmp:Bitmap;
        private var pieces:Vector.<Piece> = new Vector.<Piece>();        

        private var mouse_down:Boolean;
        
        public function Main() {
            setup();            
            addChild(new Stats());
        }      

        private function setup():void {
            bmd = new BitmapData(BMD_WIDTH, BMD_HEIGHT, true, 0x00FFFFFF);
            bmp = new Bitmap(bmd);
            addChild(bmp);            

            var _cols:uint = 30;
            var _rows:uint = 30;            
            offset_x = (BMD_WIDTH - _cols * PIECE_WIDTH) / 2;
            offset_y = (BMD_HEIGHT - _rows * PIECE_HEIGHT) / 2;            

            var _x:Number, _y:Number;
            var _p:Piece;
            var _c:uint;
            for (var i:int = 0; i < _cols; i++) {
                for (var j:int = 0; j < _rows; j++) {
                    _x = i * PIECE_WIDTH;
                    _y = j * PIECE_HEIGHT;                    
                    _c = 255 << 24 | i / _cols * 255 << 16 | j / _rows * 255 << 8 | 125;
                    
                    _p = new Piece(randomRange( -offset_x, BMD_WIDTH - offset_x), randomRange( -offset_y, BMD_HEIGHT - offset_y), _c);
                    _p.damp = randomRange(50, 150) / 1000;
                    _p.home_force = randomRange(30, 90) / 10000;
                    _p.setTarLoc(_x, _y);

                    pieces.push(_p);
                }
            }
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        }        

        private function onEnterFrame(e:Event):void {
            var _full_rect:Rectangle = new Rectangle(0, 0, BMD_WIDTH, BMD_HEIGHT);
            bmd.lock();
            bmd.fillRect(_full_rect, 0x00FFFFFF);            

            var _r:Rectangle = new Rectangle(0, 0, PIECE_WIDTH, PIECE_HEIGHT);
            var _p:Piece;
            var _force:int = -2;
            
            if (mouse_down) {
                mouse_down = false;
                _force = -200;
            }

            for (var i:uint = 0; i < pieces.length; i++) {
                _p = pieces[i];
                _p.addForce(mouseX - offset_x, mouseY - offset_y, 0, 100, _force);
                _p.seekHome(_p.home_force);    //.006
                _p.addDamping(_p.damp);    //.09
                _p.update();

                _r.x = _p.loc.x + offset_x;
                _r.y = _p.loc.y + offset_y;
                
                bmd.fillRect(_r, _p.color);
            }
            
            bmd.unlock();
        }
        
        private function onMouseDown(e:MouseEvent):void {
            mouse_down = true;
        }
        
        private function randomRange(_min:int, _max:int):int {
            return Math.floor(Math.random() * (_max - _min + 1)) + _min
        }
        
    }
    
}

import flash.geom.Vector3D;

class Piece {        

    public var loc:Vector3D = new Vector3D();    //позиция
    private var vel:Vector3D = new Vector3D();    //скорость
    private var acc:Vector3D = new Vector3D();    //ускорение
    private var tar_loc:Vector3D = new Vector3D();    //цель
    
    public var color:uint;
    public var damp:Number;
    public var home_force:Number;
    
    public function Piece(_x:Number, _y:Number, _color:uint) {
        loc.x = _x;
        loc.y = _y;
        color = _color;
    }
    
    public function update():void {
        var _dist:Vector3D = tar_loc.subtract(loc);
        
        vel = vel.add(acc);
        loc = loc.add(vel);
        
        acc.x = acc.y = acc.z = 0;
    }
   
    public function setTarLoc(_x:Number, _y:Number, _z:Number = 0):void {
        tar_loc.x = _x;
        tar_loc.y = _y;
    }
    
    public function addForce(_x:Number, _y:Number, _z:Number, _min_dist:Number, _scale:Number):void {
        var _tar_loc:Vector3D = new Vector3D(_x, _y, _z);
        var _dir:Vector3D = _tar_loc.subtract(loc);
        var _d:Number = _dir.length;        

        if (_d > 0 && _d < _min_dist) {
            var _pct:Number = 1 - (_d / _min_dist);
            _dir.normalize();
            _dir.scaleBy(_scale * _pct);
            acc = acc.add(_dir);
        }
    }
    
    public function seekHome(_scale:Number):void {
        var _dir:Vector3D = tar_loc.subtract(loc);
        _dir.scaleBy(_scale);
        acc = acc.add(_dir);
    }
    
    public function addDamping(_damp:Number):void {
        var _dir:Vector3D = acc.subtract(vel);
        _dir.scaleBy(_damp);
        acc = acc.add(_dir);
    }    

    public function toString():String {
        return "x = " + loc.x + " y = " + loc.y
    }
    
}