cad>
importFlash.events.MouseEvent; stop(); startButton.addEventListener(MouseEvent.CLICK,clickStart); functionclickStart(event:MouseEvent){ gotoAndStop("play"); } |
stop(); startAirRaid(); |
playAgainButton.addEventListener(MouseEvent.CLICK,clickPlayAgain); functionclickPlayAgain(event:MouseEvent){ gotoAndStop("play"); } |
parent.removeChild(this); stop(); |
#p#副标题#e#
package { import flash.display.MovieClip;; import flash.display.SimpleButton; import flash.events.KeyboardEvent; import flash.utils.TIMer; import flash.events.TimerEvent; import flash.text.TextField; import flash.events.Event; public class AirRaid extends MovieClip { private var aagun:AAGun;//火炮 private var airplanes:Array;//飞机数组 private var bullets:Array;//子弹数组 public var leftArrow, rightArrow:Boolean; private var nextPlane:Timer;//不定时生成飞机的计时器 private var shotsLeft:int;//得分文本 private var shotsHit:int;//子弹数文本 public function startAirRaid() { // 初始化得分数和子弹数 shotsLeft = 20; shotsHit = 0; showGameScore(); // 生成炮加入到舞台上 aagun = new AAGun(); addChild(aagun); // 生成飞机、子弹数组 airplanes = new Array(); bullets = new Array(); // 键盘按下、释放事件侦听器 stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction); stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction); // 进入帧事件侦听器,检测子弹击中飞机的碰撞检测。 addEventListener(Event.ENTER_FRAME,checkForHits); // 生成下一架飞机 setNextPlane(); } //不定时生成飞机 public function setNextPlane() { //1000毫秒至2000毫秒之间生成一架飞机 nextPlane = new Timer(1000 + Math.random() * 1000,1); // nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane); nextPlane.start(); } public function newPlane(event:TimerEvent) { // 随机的边、速度和高度 if (Math.random() > .5) { var side:String = "left"; } else { side = "right"; } var altitude:Number = Math.random()*50+20; var speed:Number = Math.random()*150+150; // 生成飞机 var p:Airplane = new Airplane(side,speed,altitude); addChild(p); airplanes.push(p); // set time for next plane setNextPlane(); } // 碰撞检测 public function checkForHits(event:Event) { for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){ for (var airplaneNum:int=airplanes.length-1;airplaneNum>=0;airplaneNum--) { if (bullets[bulletNum].hitTestObject(airplanes[airplaneNum])) { airplanes[airplaneNum].planeHit(); bullets[bulletNum].deleteBullet(); shotsHit++; showGameScore(); break; } } } if ((shotsLeft == 0) && (bullets.length == 0)) { endGame(); } } // 按下键盘 public function keyDownFunction(event:KeyboardEvent) { if (event.keyCode == 37) { leftArrow = true; } else if (event.keyCode == 39) { rightArrow = true; } else if (event.keyCode == 32) { fireBullet(); } } // 释放键盘 public function keyUpFunction(event:KeyboardEvent) { if (event.keyCode == 37) { leftArrow = false; } else if (event.keyCode == 39) { rightArrow = false; } } // 生成新的子弹 public function fireBullet() { if (shotsLeft <= 0) return; var b:Bullet = new Bullet(aagun.x,aagun.y,-300); addChild(b); bullets.push(b); shotsLeft--; showGameScore(); } public function showGameScore() { showScore.text = String("得分: "+shotsHit); showShots.text = String("剩余子弹: "+shotsLeft); } // 从数组获取飞机 public function removePlane(plane:Airplane) { for(var i in airplanes) { if (airplanes[i] == plane) { airplanes.splice(i,1); break; } } } // 获取数组的一个子弹 public function removeBullet(bullet:Bullet) { for(var i in bullets) { if (bullets[i] == bullet) { bullets.splice(i,1); break; } } } // 游戏结束,移除界面上的东西。 public function endGame() { // 移除飞机 for(var i:int=airplanes.length-1;i>=0;i--) { airplanes[i].deletePlane(); } airplanes = null; aagun.deleteGun(); aagun = null; // 移除侦听器 stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction); stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction); removeEventListener(Event.ENTER_FRAME,checkForHits); nextPlane.stop(); nextPlane = null; gotoAndStop("gameover"); } } } |
package { import flash.display.*; import flash.events.*; import flash.utils.getTimer; public class AAGun extends MovieClip { static const speed:Number = 150.0; private var lastTime:int; // 控制移动的Timer public function AAGun() { // 枪的初始位置 this.x = 275; this.y = 340; //运动 addEventListener(Event.ENTER_FRAME,moveGun); } public function moveGun(event:Event) { // 得到时差 var timePassed:int = getTimer() - lastTime; lastTime += timePassed; // 现在的位置 var newx = this.x; // 移动到左边 if (MovieClip(parent).leftArrow) { newx -= speed*timePassed / 1000; } // 移动到右边 if (MovieClip(parent).rightArrow) { newx += speed*timePassed / 1000; } // 检测边界 if (newx < 10) newx = 10; if (newx > 540) newx = 540; // 更新位置 this.x = newx; } // 移除屏幕上的炮和事件 public function deleteGun() { parent.removeChild(this); removeEventListener(Event.ENTER_FRAME,moveGun); } } } |
package { import flash.display.MovieClip; import flash.events.TimerEvent; import flash.utils.Timer; import flash.utils.getTimer; import flash.events.Event; public class Airplane extends MovieClip { private var dx:Number; // 飞机的速度、方向 private var lastTime:int; // 控制移动的Timer //左右移动 public function Airplane(side:String, speed:Number, altitude:Number) { if (side == "left") { this.x = -50; // start to the left dx = speed; // fly left to right this.scaleX = -1; // reverse } else if (side == "right") { this.x = 600; // start to the right dx = -speed; // fly right to left this.scaleX = 1; // not reverse } this.y = altitude; // vertical position this.gotoAndStop(Math.floor(Math.random()*5+1)); // set up animation addEventListener(Event.ENTER_FRAME,movePlane); lastTime = getTimer(); } //检测边界 public function movePlane(event:Event) { // get time passed var timePassed:int = getTimer()-lastTime; lastTime += timePassed; // move plane this.x += dx*timePassed/1000; // check to see if off screen if ((dx < 0) && (x < -50)) { deletePlane(); } else if ((dx > 0) && (x > 600)) { deletePlane(); } } // 检测碰撞,子弹击中飞机跳转到帧标签"explode"演示爆炸动画。 public function planeHit() { removeEventListener(Event.ENTER_FRAME,movePlane); MovieClip(parent).removePlane(this); gotoAndPlay("explode"); } // 移除舞台上的飞机和事件 public function deletePlane() { removeEventListener(Event.ENTER_FRAME,movePlane); MovieClip(parent).removePlane(this); parent.removeChild(this); } } } |
package { import flash.display.MovieClip; import flash.events.Event; import flash.utils.getTimer; import flash.utils.Timer; import flash.events.TimerEvent; public class Bullet extends MovieClip { private var dy:Number; // 子弹的速度、方向。 private var lastTime:int; public function Bullet(x,y:Number, speed: Number) { // 初始位置 this.x = x; this.y = y; // 得到速度 dy = speed; // 动画 lastTime = getTimer(); addEventListener(Event.ENTER_FRAME,moveBullet); } public function moveBullet(event:Event) { // 得到时差 var timePassed:int = getTimer()-lastTime; lastTime += timePassed; // 子弹运动 this.y += dy*timePassed/1000; // 子弹越过屏幕的顶端 if (this.y < 0) { deleteBullet(); } } // 移除舞台的子弹和事件 public function deleteBullet() { MovieClip(parent).removeBullet(this); parent.removeChild(this); removeEventListener(Event.ENTER_FRAME,moveBullet); } } } |
#p#副标题#e#