【AS3代码】打砖块

package
{
     import flash.display. MovieClip;
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.text.TextField;
    
     public  class Main  extends Sprite
    {
         private  var vx: Number = 10;
         private  var vy: Number = 10;
        
         public const BOARD_WIDTH: Number = 60;
         public const BOARD_HEIGHT: Number = 15;
        
         public const H_GAP: Number = 8;
         public const V_GAP: Number = 15;
        
         public const SPEED: Number = 20;
        
         private  var boardArr:Array = new Array();
        
         private  var newBoard:myBoard;
         private  var ball:Ball = new Ball();
         private  var board:myBoard;
        
         private  var score:TextField;
         private  var num: Number = new  Number();
        
         private  var an:anniu;
        
         public  function Main()
        {
            init();
        }
         private  function init():void
        {
            num = 0;
            
             // 显示球
            ball.x = 200;
            ball.y = 300;
            this.addChild(ball);
            
             // 显示底板
            board = new myBoard();
            board.x = stage.stageWidth / 2;
            board.y = stage.stageHeight - board.height;
            this.addChild(board);
            
             // 显示方块
             for( var i:int = 0; i < 7; i++)
            {
                 for( var j:int = 0; j < 6; j++)
                {
                    newBoard = new myBoard();
                    newBoard.width = BOARD_WIDTH;
                    newBoard.height = BOARD_HEIGHT;
                    newBoard.x = 8 + BOARD_WIDTH / 2 + i * BOARD_WIDTH + i * H_GAP;
                    newBoard.y = 20 + BOARD_HEIGHT / 2 + j * BOARD_HEIGHT + j * V_GAP;
                    addChild(newBoard);
                    boardArr.push(newBoard);
                }
            }
            
             // 显示得分
            score = new TextField();
            score.text = "得分:" +  String(num);
            score.x = 5;
            score.y = stage.stageHeight - 50;
            this.addChild(score);
            
             // 显示按钮
            an = new anniu();
            an.x = stage.stageWidth - 50;
            an.y = stage.stageHeight - 80;
            an.buttonMode =  true;
            this.addChild(an);
            
            an.addEventListener(MouseEvent.CLICK, onClick);
                        
            stage.addEventListener(Event.ENTER_FRAME, onEF);
        }
        
         private  function onClick(e:MouseEvent):void
        {
            
            stage.removeChild(score);
            stage.removeChild(newBoard);            
            init();
        }
        
         private  function onEF(event:Event):void
        {
             var dis: Number = this.mouseX - board.x;
            
             // 底板跟随鼠标
            board.x = this.mouseX;
            
             // 限制底板不要出左右边界
             if((board.x - board.width / 2) <= 0)
            {
                board.x = board.width / 2;
            }
            
             else  if((board.x + board.width / 2) >= stage.stageWidth)
            {
                board.x = stage.stageWidth - board.width / 2;
            }
            
             // 球自由滚动
            ball.x += vx;
            ball.y += vy;
            
             // 若球接触了右边界
             if((ball.x + ball.width / 2) > stage.stageWidth)
            {
                vx *= -1;
            }
             // 接触了左边界
             else  if((ball.x - ball.width / 2) < 0)
            {
                vx *= -1;
            }
             // 若球接触了下边界
             else  if((ball.y + ball.height / 2) > stage.stageHeight)
            {
                 // vy *= -1;
                
                stage.removeChild(ball);
                
                 // 更新得分
                num--;
                score.text = "得分:" +  String(num);
            }
             // 接触了上边界
             else  if((ball.y - ball.height / 2) < 0)
            {
                vy *= -1;
            }
            
             // 若方块碰到了小球
             if(board.hitTestObject(ball))
            {
                vy *= -1;
            }
            
             // 若球碰到砖块,就移除砖块
             for( var i:int=0;i<boardArr.length;i++)
            {
                 if(ball.hitTestObject(boardArr[i]))
                {
                    this.removeChild(boardArr[i]);
                    boardArr.splice(i, 1);
                    vy *= -1;
                    
                     // 更新得分
                    num++;
                    score.text = "得分:" +  String(num);
                }
                    
            }
        }
    }
}

你可能感兴趣的:(as3)