[AS3]坚持30秒,as3小游戏源代码示例
[AS3]坚持30秒,as3小游戏源代码示例,flash小游戏源代码
[AS3]坚持30秒,as3小游戏源代码示例
- 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);
- //CuPlayer.com提示:放置小星星
- for(var i:int = 0; i < starNum; i++)
- {
- makeStars();
- }
- //CuPlayer.com提示:按键改变小球移动速度
- stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
- stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
- //球的移动事件
- stage.addEventListener(Event.ENTER_FRAME, moveBall);
- //CuPlayer.com提示:小星星的移动事件
- 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;
- }
- }
- //CuPlayer.com提示:移动小星星
- 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;
- }
- //CuPlayer.com提示:移动小球
- 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;
- //CuPlayer.com提示:往上飞
- case 1:
- star.x = Math.random() * stage.stageWidth;
- star.y = stage.stageHeight;
- star.speedX = Math.random() * 4 * xyDirection;
- star.speedY = -Math.random() * 4;
- break;
- //CuPlayer.com提示:往左飞
- 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);
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...