是男人就坚持30秒

package
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    
    public class Main extends Sprite
    {
        private var ballXSpeed:Number = 0;            //小球X轴速度
        private var ballYSpeed:Number = 0;            //小球Y轴速度
        private var starNum:int = 10;                //星星数量
        private var starArray:Array; //保存星星的数组
        private var ball:mcBall;
        
        public function Main()
        {
            init();
        }
        private function init():void
        {
            starArray = new Array();
            ball = new mcBall();
            ball.x = stage.stageWidth / 2;
            ball.y = stage.stageHeight / 2;
            this.addChild(ball);
            
            //放置小星星
            for(var i:int = 0; i < starNum; i++)
            {
                makeStars();
            }
            
            //按键改变小球移动速度
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
            
            //球的移动事件
            stage.addEventListener(Event.ENTER_FRAME, moveBall);
            
            //小星星的移动事件
            stage.addEventListener(Event.ENTER_FRAME, moveStar);
        }
        
        private function onKeyDownHandler(evt:KeyboardEvent):void
        {
            if(evt.keyCode == Keyboard.LEFT)
            {
                ballXSpeed = -4;
            }
            else if(evt.keyCode == Keyboard.RIGHT)
            {
                ballXSpeed = 4;
            }
            else if(evt.keyCode == Keyboard.UP)
            {
                ballYSpeed = -4;
            }
            else if(evt.keyCode == Keyboard.DOWN)
            {
                ballYSpeed = 4;
            }
        }
        
        //移动小星星
        private function moveStar(evt:Event):void
        {
            for(var i:uint = 0; i < starArray.length; i++)
            {
                starArray[i].x += starArray[i].speedX;
                starArray[i].y += starArray[i].speedY;    
                
                if(starArray[i].x >= stage.stageWidth + 50 || starArray[i].x <= -50 || starArray[i].y >= stage.stageHeight +50 || starArray[i].y <= -50)
                {
                    removeChild(starArray[i]);
                    starArray.splice(i,1);
                    i -= 1;
                    makeStars();
                }
            }
            
            
        }
        
        private function onKeyUpHandler(evt:KeyboardEvent):void
        {
            ballXSpeed = 0;
            ballYSpeed = 0;
        }
        
        //移动小球
        private function moveBall(evt:Event):void
        {
            ball.x += ballXSpeed;
            ball.y += ballYSpeed;
            
            //如果碰到墙壁就反弹,直接改变速度的方向
            if(ball.x >= stage.stageWidth - ball.width / 2)
            {
                ball.x = stage.stageWidth - ball.width / 2 - 0.1;
            }
            else if(ball.x <= 0)
            {
                ball.x = 0;
            }
            else if(ball.y >= stage.stageHeight - ball.height / 2)
            {
                ball.y = stage.stageHeight - ball.height / 2 - 0.1;
            }
            else if(ball.y <= 0)
            {
                ball.y = 0;
            }
            
            //判断小球是否和星星碰撞
            for(var i:uint = 0; i < starArray.length; i++)
            {
                if(ball.hitTestObject(starArray[i]))
                {
                    trace("很不幸!你撞到星星了!");
                    stage.removeEventListener(Event.ENTER_FRAME,moveBall);
                    stage.removeEventListener(Event.ENTER_FRAME, moveStar);
                    break;
                }
            }
        }
        
        //放置小星星
        private function makeStars():void
        {
            var star:Star = new Star();
            
            
            //随机产生星星初始方向(值范围0-3)
            var starDirection:int = Math.floor(Math.random() * 4);
            var temp:int = Math.floor(Math.random() * 2);    //0,1
            var xyDirection:int = (temp == 0) ? 1 : -1;        //随机获得往x或y方向飞
            
            switch(starDirection)
            {
                //往下飞
                case 0:
                    star.x = Math.random() * stage.stageWidth;
                    star.y = 0;
                    star.speedX = Math.random() * 4 * xyDirection;
                    star.speedY = Math.random() * 4;
                    break;
                //往上飞
                case 1:
                    star.x = Math.random() * stage.stageWidth;
                    star.y = stage.stageHeight;
                    star.speedX = Math.random() * 4 * xyDirection;
                    star.speedY = -Math.random() * 4;
                    break;
                //往左飞
                case 2:
                    star.x = stage.stageWidth;
                    star.y = Math.random() * stage.stageHeight;
                    star.speedX = -Math.random() * 4;
                    star.speedY = Math.random() * 4 * xyDirection;
                    break;
                //往右飞
                case 3:
                    star.x = 0;
                    star.y = Math.random() * stage.stageHeight;
                    star.speedX = Math.random() * 4;
                    star.speedY = Math.random() * 4 * xyDirection;
                    break;
            }
            
            this.addChild(star);
            starArray.push(star);
            
        }
    }
}

你可能感兴趣的:(是男人就坚持30秒)