Drawy Thing

by forresto
2010-9-9
♥0 | Line 35 | Modified 2010-09-09 17:27:20 | MIT License
play

ActionScript3 source code

/**
 * Copyright forresto ( http://wonderfl.net/user/forresto )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aT0l
 */

package {
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    
    public class DrawyThing extends Sprite {
        private var points:Array = new Array();
        private var max:int = 20; 
        
        public function DrawyThing() {
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            stage.addEventListener(MouseEvent.CLICK, onMouseClick);
        }
        private function onMouseMove(e:MouseEvent):void {
            if (points.length > max) {
                points.shift();
                points.shift();
            }
            points.push(mouseX);
            points.push(mouseY);
            
            graphics.lineStyle(1, 0, .1);
            for(var i:int=0; i<points.length; i+=2){
                graphics.moveTo(points[i],points[i+1]);
                graphics.lineTo(mouseX,mouseY);
            }
            if (points.length>=4) {
                graphics.lineStyle(2, 0, .5);
                graphics.moveTo(points[points.length-4],points[points.length-3]);
                graphics.lineTo(mouseX,mouseY);
            }
        }
        private function onMouseClick(e:MouseEvent):void {
            points = new Array();
            graphics.clear();
        }

    }
}