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: Mouse Drag

simplified, propogated to object-design for modularity,
implemented independent math solution for dragging,
demonstrated readability...
Get Adobe Flash player
by hemingway 20 Nov 2012
    Embed
/**
 * Copyright hemingway ( http://wonderfl.net/user/hemingway )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/vF9w
 */

/* forked from DakeNemui's Mouse Drag
 * 
 * simplified, propogated to object-design for modularity,
 * implemented independent math solution for dragging,
 * demonstrated readability...
 *
 */

package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    
    [SWF(frameRate = 60, width = 460, height = 345)]
    public class FlashTest extends Sprite
    {
        public function FlashTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            if (stage) _init() else addEventListener(Event.ADDED_TO_STAGE, addedToStage);
        }
        
        public function _init() :void
        {
            for (var $children:Number = 0; $children < 3; $children++)
            { addChild(new DragSprite(30, 115 + Math.random() * 230, 91.25 + Math.random() * 172.5)) }
            
            graphics.clear     ();
            graphics.lineStyle (1, 0, 0.75);
            graphics.drawRect  (0, 0, 459, 344);
        }
        
        public function addedToStage($e:Event) :void
        { _init() }  
    }
}

import flash.display.*;
import flash.events.*;
import flash.geom.*;

class DragSprite extends Sprite
{
    protected var _c:Number;
    protected var _a:Number;
    protected var _x:Number;
    protected var _y:Number;
    protected var _r:Number;
    
    public function DragSprite($r:Number = 30, $x:Number = 230, $y:Number = 172.5, $a:Number = 0.75, $c:Number = 0)
    {
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
        
        _c = $c;
        _a = $a;
        _x = $x;
        _y = $y;
        _r = $r;
    }
    
    public function _init() :void
    {
        graphics.clear      ();
        graphics.beginFill  (_c, _a);
        graphics.drawCircle (_x, _y, _r);
        graphics.endFill    ();
    }
    
    public function addedToStage($e:Event) :void
    {
        _init();
        
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }
    
    internal var oldPoint :Point = new Point();
    internal var newPoint :Point = new Point();
    internal var clcPoint :Point = new Point();
    
    public function onMouseDown($e:MouseEvent) :void
    {
        stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        
        oldPoint = new Point($e.stageX, $e.stageY);
    }
    
    public function onMouseMove($e:MouseEvent) :void
    {
        newPoint = new Point($e.stageX, $e.stageY);
        clcPoint = Point(newPoint.subtract(oldPoint));
        
        x = (x + clcPoint.x);
        y = (y + clcPoint.y);
        
        oldPoint = newPoint;
    }
    
    public function onMouseUp($e:MouseEvent) :void
    {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    }
    
    public override function get x() :Number
    { return _x }
    
    public override function get y() :Number
    { return _y }
    
    public override function set x($value:Number) :void
    { _x = $value; _init(); }
    
    public override function set y($value:Number) :void
    { _y = $value; _init(); }
}