【AS3代码】模仿现实世界中(地球重力)的甩球游戏

package
{
    import com.ui.Ball;
    
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;

    public class Main extends Sprite
    {    
        private var ball:Ball;
        private var vx:Number;                //x轴运动向量
        private var vy:Number;                //y轴运动向量
        private var bounce:Number = -0.7;    //弹力
        private var gravity:Number = 2.5;    //地球重力
        private var oldX :Number;
        private var oldY:Number;
        
        public function Main():void
        {
            init();
        }
        private function init():void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            ball = new Ball();
            ball.x = stage.stageWidth / 2;
            ball.y = stage.stageHeight / 2;
            vx = Math.random() * 10 - 5;
            vy = -10;
            this.addChild(ball);
            
            ball.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        private function onEnterFrame(event:Event):void
        {
            vy += gravity;    //重力+到Y轴上
            ball.x += vx;    //x运动向量+到x轴上
            ball.y += vy;
            var left:Number = 0;
            var right:Number = stage.stageWidth;
            var top:Number = 0;
            var bottom:Number = stage.stageHeight;
            
            if(ball.x + ball.width / 2 > right)
            {
                ball.x = right - ball.width / 2;
                vx *= bounce;    //反弹回来
            }
            else if(ball.x - ball.width / 2 < left)
            {
                ball.x = left + ball.width / 2;
                vx *= bounce;
            }
            
            if(ball.y + ball.height / 2 > bottom)
            {
                ball.y = bottom - ball.height / 2;
                vy *= bounce;    //反弹回来
            }
            else if(ball.y - ball.height / 2 < top)
            {
                ball.y = top + ball.height / 2;
                vx *= bounce;
            }
        }
        
        private function onMouseDown(event:MouseEvent):void
        {
            oldX = ball.x;
            oldY = ball.y;
            vx = 0;
            vy = 0;
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            ball.startDrag();
            stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            stage.addEventListener(Event.ENTER_FRAME, trackVelocity);
        }
        private function onMouseUp(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            ball.stopDrag();
            stage.removeEventListener(Event.ENTER_FRAME, trackVelocity);
            stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);            
        }
        private function trackVelocity(event:Event):void
        {
            vx = ball.x - oldX;
            vy = ball.y - oldY;
            oldX = ball.x;
            oldY = ball.y;
        }
    }
}
原文地址:https://www.cnblogs.com/kingfly/p/2586223.html