SuperBall

by umhr
♥0 | Line 82 | Modified 2015-11-04 19:08:07 | MIT License
play

ActionScript3 source code

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

package 

{

    

    import flash.display.Shape;

    import flash.display.Sprite;

    import flash.display.StageAlign;

    import flash.display.StageScaleMode;

    import flash.events.Event;

    import flash.events.MouseEvent;

    import flash.geom.Point;

    /**

     * ...

     * @author umhr

     */

    [SWF(width = 465, height = 465, backgroundColor = 0x000000, frameRate = 60)]

    public class Conteiner extends Sprite 

    {

        

        private var _mouseMovePosition:Point = new Point();

        private var _currentDistance:Point = new Point();

        private var _isMouseDown:Boolean;

        private var _circle:Shape = new Shape();

        private var _stageWidth:int;

        private var _stageHeight:int;

        public function Conteiner() 

        {

            init();

        }

        private function init():void 

        {

            if (stage) onInit();

            else addEventListener(Event.ADDED_TO_STAGE, onInit);

        }



        private function onInit(event:Event = null):void 

        {

            removeEventListener(Event.ADDED_TO_STAGE, onInit);

            // entry point

            

            stage.scaleMode = StageScaleMode.NO_SCALE;

            stage.align = StageAlign.TOP_LEFT;

            

            _stageWidth = stage.stageWidth;

            _stageHeight = stage.stageHeight;

            

            _circle.graphics.beginFill(0xFF0000, 1);

            _circle.graphics.drawCircle(0, 0, 25);

            _circle.graphics.endFill();

            _circle.x = 200;

            _circle.y = 200;

            addChild(_circle);

            

            stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);

            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);

            addEventListener(Event.ENTER_FRAME, enterFrame);

        }

        

        private function mouseUp(e:MouseEvent):void 

        {

            _isMouseDown = false;

        }

        

        private function mouseDown(e:MouseEvent):void 

        {

            _mouseMovePosition.x = mouseX;

            _mouseMovePosition.y = mouseY;

            _isMouseDown = true;

        }

        

        private function enterFrame(e:Event):void 

        {

            

            var cX:Number = _circle.x;

            var cY:Number = _circle.y;

            

            if (_isMouseDown) {

                

                _currentDistance.x = mouseX - _mouseMovePosition.x;

                _currentDistance.y = mouseY - _mouseMovePosition.y;

                

                _mouseMovePosition.x = mouseX;

                _mouseMovePosition.y = mouseY;

            }else {

                _currentDistance.x *= 0.98;

                _currentDistance.y *= 0.98;

                

            }

            

            cX += _currentDistance.x;

            cY += _currentDistance.y;

            

            

            

            if(0 < cX && cX < _stageWidth){

                _circle.x = cX;

            }else {

                _currentDistance.x = -_currentDistance.x;

            }

            if(0 < cY && cY < _stageHeight){

                _circle.y = cY;

            }else {

                _currentDistance.y = -_currentDistance.y;

            }

            

        }

    }

    

}