·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]坚持30秒,as3小游戏源代码示例

时间:2013-03-01 10:55cnblogs.com
[AS3]坚持30秒,as3小游戏源代码示例,flash小游戏源代码

[AS3]坚持30秒,as3小游戏源代码示例

  1. package 
  2.     import flash.display.MovieClip; 
  3.     import flash.display.Sprite; 
  4.     import flash.events.Event; 
  5.     import flash.events.KeyboardEvent; 
  6.     import flash.ui.Keyboard; 
  7.      
  8.     public class Main extends Sprite 
  9.     { 
  10.         private var ballXSpeed:Number = 0;            //小球X轴速度 
  11.         private var ballYSpeed:Number = 0;            //小球Y轴速度 
  12.         private var starNum:int = 10;                //星星数量 
  13.         private var starArray:Array;    //保存星星的数组 
  14.         private var ball:mcBall; 
  15.          
  16.         public function Main() 
  17.         { 
  18.             init(); 
  19.         } 
  20.         private function init():void 
  21.         { 
  22.             starArray = new Array(); 
  23.             ball = new mcBall(); 
  24.             ball.x = stage.stageWidth / 2; 
  25.             ball.y = stage.stageHeight / 2; 
  26.             this.addChild(ball); 
  27.              
  28.             //CuPlayer.com提示:放置小星星 
  29.             for(var i:int = 0; i < starNum; i++) 
  30.             { 
  31.                 makeStars(); 
  32.             } 
  33.              
  34.             //CuPlayer.com提示:按键改变小球移动速度 
  35.             stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler); 
  36.             stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler); 
  37.              
  38.             //球的移动事件 
  39.             stage.addEventListener(Event.ENTER_FRAME, moveBall); 
  40.              
  41.             //CuPlayer.com提示:小星星的移动事件 
  42.             stage.addEventListener(Event.ENTER_FRAME, moveStar); 
  43.         } 
  44.          
  45.         private function onKeyDownHandler(evt:KeyboardEvent):void 
  46.         { 
  47.             if(evt.keyCode == Keyboard.LEFT) 
  48.             { 
  49.                 ballXSpeed = -4; 
  50.             } 
  51.             else if(evt.keyCode == Keyboard.RIGHT) 
  52.             { 
  53.                 ballXSpeed = 4
  54.             } 
  55.             else if(evt.keyCode == Keyboard.UP) 
  56.             { 
  57.                 ballYSpeed = -4; 
  58.             } 
  59.             else if(evt.keyCode == Keyboard.DOWN) 
  60.             { 
  61.                 ballYSpeed = 4
  62.             } 
  63.         } 
  64.          
  65.         //CuPlayer.com提示:移动小星星 
  66.         private function moveStar(evt:Event):void 
  67.         { 
  68.             for(var i:uint = 0; i < starArray.length; i++) 
  69.             { 
  70.                 starArray[i].x += starArray[i].speedX; 
  71.                 starArray[i].y += starArray[i].speedY;     
  72.                  
  73.                 if(starArray[i].x >= stage.stageWidth + 50 || starArray[i].x <= -50 || starArray[i].y >= stage.stageHeight +50 || starArray[i].y <= -50) 
  74.                 { 
  75.                     removeChild(starArray[i]); 
  76.                     starArray.splice(i,1); 
  77.                     i -1
  78.                     makeStars(); 
  79.                 } 
  80.             } 
  81.              
  82.              
  83.         } 
  84.          
  85.         private function onKeyUpHandler(evt:KeyboardEvent):void 
  86.         { 
  87.             ballXSpeed = 0
  88.             ballYSpeed = 0
  89.         } 
  90.          
  91.         //CuPlayer.com提示:移动小球 
  92.         private function moveBall(evt:Event):void 
  93.         { 
  94.             ball.x += ballXSpeed; 
  95.             ball.y += ballYSpeed; 
  96.              
  97.             //如果碰到墙壁就反弹,直接改变速度的方向 
  98.             if(ball.x >= stage.stageWidth - ball.width / 2) 
  99.             { 
  100.                 ball.x = stage.stageWidth - ball.width / 2 - 0.1; 
  101.             } 
  102.             else if(ball.x <= 0) 
  103.             { 
  104.                 ball.x = 0
  105.             } 
  106.             else if(ball.y >= stage.stageHeight - ball.height / 2) 
  107.             { 
  108.                 ball.y = stage.stageHeight - ball.height / 2 - 0.1; 
  109.             } 
  110.             else if(ball.y <= 0) 
  111.             { 
  112.                 ball.y = 0
  113.             } 
  114.              
  115.             //判断小球是否和星星碰撞 
  116.             for(var i:uint = 0; i < starArray.length; i++) 
  117.             { 
  118.                 if(ball.hitTestObject(starArray[i])) 
  119.                 { 
  120.                     trace("很不幸!你撞到星星了!"); 
  121.                     stage.removeEventListener(Event.ENTER_FRAME,moveBall); 
  122.                     stage.removeEventListener(Event.ENTER_FRAME, moveStar); 
  123.                     break; 
  124.                 } 
  125.             } 
  126.         } 
  127.          
  128.         //放置小星星 
  129.         private function makeStars():void 
  130.         { 
  131.             var star:Star = new Star(); 
  132.              
  133.              
  134.             //随机产生星星初始方向(值范围0-3) 
  135.             var starDirection:int = Math.floor(Math.random() * 4); 
  136.             var temp:int = Math.floor(Math.random() * 2);    //0,1 
  137.             var xyDirection:int = (temp == 0) ? 1 : -1;        //随机获得往x或y方向飞 
  138.              
  139.             switch(starDirection) 
  140.             { 
  141.                 //往下飞 
  142.                 case 0: 
  143.                     star.x = Math.random() * stage.stageWidth; 
  144.                     star.y = 0
  145.                     star.speedX = Math.random() * 4 * xyDirection; 
  146.                     star.speedY = Math.random() * 4; 
  147.                     break; 
  148.                 //CuPlayer.com提示:往上飞 
  149.                 case 1: 
  150.                     star.x = Math.random() * stage.stageWidth; 
  151.                     star.y = stage.stageHeight; 
  152.                     star.speedX = Math.random() * 4 * xyDirection; 
  153.                     star.speedY = -Math.random() * 4; 
  154.                     break; 
  155.                 //CuPlayer.com提示:往左飞 
  156.                 case 2: 
  157.                     star.x = stage.stageWidth; 
  158.                     star.y = Math.random() * stage.stageHeight; 
  159.                     star.speedX = -Math.random() * 4; 
  160.                     star.speedY = Math.random() * 4 * xyDirection; 
  161.                     break; 
  162.                 //往右飞 
  163.                 case 3: 
  164.                     star.x = 0
  165.                     star.y = Math.random() * stage.stageHeight; 
  166.                     star.speedX = Math.random() * 4; 
  167.                     star.speedY = Math.random() * 4 * xyDirection; 
  168.                     break; 
  169.             } 
  170.              
  171.             this.addChild(star); 
  172.             starArray.push(star); 
  173.              
  174.         } 
  175.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器