flash on 2013-3-2

by hemingway
♥0 | Line 134 | Modified 2013-03-07 04:05:28 | MIT License
play

ActionScript3 source code

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

package
{
    import flash.display.*;
    import flash.events.*;
    
    [SWF(frameRate = 60, width = 465, height = 465)]
    public class Main extends Sprite
    {
        public function Main()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            addEventListener(Event.ADDED_TO_STAGE, addedToStage);
        }
        
        private function addedToStage($e:*) :void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
            
            addChild(new Cannon);
            
            init();
        }
        
        private function init() :void
        {
            graphics.clear();
            graphics.lineStyle(1);
            graphics.drawRect(0, 0, stage.stageWidth-1, stage.stageHeight-1);
        }
    }
}

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

class Cannon extends Sprite
{
    protected var _x :Number;
    protected var _y :Number;
    protected var _dist :Number;
    
    public var bMouseDown :Boolean;
     
    public function Cannon()
    {
        _dist = 0.75;
        
        bMouseDown = false;
        
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    }
    
    private function addedToStage($e:*) :void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
        
        init();
        
        stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        stage.addEventListener(Event.ENTER_FRAME, onFrame);
    }
    
    private function init() :void
    {
        graphics.clear();
        graphics.lineStyle(1);
        graphics.drawCircle(stage.stageWidth/2, stage.stageHeight/2, 11);
        
        initBarrel();
    }
    
    private function initBarrel() :void
    {
        var $mPt :Point = new Point();
        var $bPt :Point = new Point();
        var $calc :Point = new Point();
        
        $mPt.setTo(stage.mouseX, stage.mouseY);
        $bPt.setTo(stage.stageWidth/2, stage.stageHeight/2);
        
        $calc.setTo(Point.interpolate($bPt, $mPt, _dist).x, Point.interpolate($bPt, $mPt, _dist).y);
        
        graphics.lineStyle(1, 0, 0.33);
        graphics.moveTo($bPt.x, $bPt.y);
        graphics.lineTo($calc.x, $calc.y);
    }
    
    private function onFrame($e:*) :void
    {
        _dist -= (bMouseDown && (_dist > 0)) ? 0.025 : 0;
        init();
    }
    
    private function onMouseDown($e:MouseEvent) :void
    {
        bMouseDown = true;
    }

    private function onMouseMove($e:MouseEvent) :void
    {
        init();
    }
    
    private function onMouseUp($e:MouseEvent) :void
    {
        bMouseDown = false;
        _dist = 0.75;
        
        initBallistic();
    }
    
    public function initBallistic() :void
    {
        var $mPt :Point = new Point();
        var $bPt :Point = new Point();
        var $calc :Point = new Point();
        
        $mPt.setTo(stage.mouseX, stage.mouseY);
        $bPt.setTo(stage.stageWidth/2, stage.stageHeight/2);
        
        $calc.setTo(Point.interpolate($bPt, $mPt, 0.95).x, Point.interpolate($bPt, $mPt, 0.95).y);
        
        addChild(new Ballistic($calc.x, $calc.y));
    }
}

class Ballistic extends Sprite
{
    protected var _x :Number;
    protected var _y :Number;
    protected var _spread :Number;
    
    public function Ballistic($x:Number, $y:Number)
    {
        _x = $x;
        _y = $y;
        
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    }
    
    private function addedToStage($e:*) :void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
    
        init();
        
        addEventListener(Event.ENTER_FRAME, onFrame);
    }
    
    private function init() :void
    {
        graphics.clear();
        graphics.lineStyle(1);
        graphics.drawCircle(_x, _y, 3);
    }
    
    private function onFrame($e:*) :void
    {
        var $x :Number = (mouseX - parent.x);
        var $y :Number = (mouseY - parent.y);
        var $vX :Number = ($x > 232) ? (_x + $x/20) : (_x - $x/20);
        var $vY :Number = ($y > 232) ? (_y + $y/20) : (_y - $y/20); 
        
        _x = $vX;
        _y = $vY;
        this.rotation += 1;
        
        init();
    }

}