import flash.media.SoundMixer; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; var xDiff:Number; var yDiff:Number; var radians:Number; var degrees:Number; var birdOfPreyDegrees:Number; var birdOfPreyRadians:Number; var phaserSpeed:int = 8; var birdOfPreySpeed:int = 12; var score:int = 0; var randomStart:int; var startingPoints:Array = new Array(); startingPoints[0] = new Array(120, 120); startingPoints[1] = new Array(stage.stageWidth - 120, 120); startingPoints[2] = new Array(stage.stageHeight - 120, stage.stageWidth - 120); startingPoints[3] = new Array(120, stage.stageHeight - 120); var phaserSound:phaserWav = new phaserWav(); restartGame.visible = false; stage.addEventListener(Event.ENTER_FRAME, moveBirdOfPrey); stage.addEventListener(MouseEvent.MOUSE_MOVE, aimEnterprise); stage.addEventListener(MouseEvent.CLICK, firePhaser); stage.addEventListener(KeyboardEvent.KEY_DOWN, shieldOn); stage.addEventListener(KeyboardEvent.KEY_UP, shieldOff); restartGame.addEventListener(MouseEvent.CLICK, newGame); function newGame(evt:MouseEvent):void { stage.addEventListener(Event.ENTER_FRAME, moveBirdOfPrey); stage.addEventListener(MouseEvent.MOUSE_MOVE, aimEnterprise); stage.addEventListener(MouseEvent.CLICK, firePhaser); stage.addEventListener(KeyboardEvent.KEY_DOWN, shieldOn); stage.addEventListener(KeyboardEvent.KEY_UP, shieldOff); createLives(); birdOfPreySpeed = 12; setBirdOfPrey(); score = 0; updateScore(); restartGame.visible = false; } function shieldOn(evt:KeyboardEvent):void { if(evt.keyCode == 32) { enterprise.gotoAndStop("shields"); } } function shieldOff(evt:KeyboardEvent):void { if(evt.keyCode == 32) { enterprise.gotoAndStop("noShields"); } } function aimEnterprise(evt:MouseEvent):void { xDiff = mouseX - enterprise.x; yDiff = mouseY - enterprise.y; radians = Math.atan2(yDiff, xDiff); degrees = radians * 180 / Math.PI; enterprise.rotation = degrees; } function firePhaser(evt:MouseEvent):void { if(evt.target.name == null || evt.target.name == "enterprise") { if(enterprise.currentLabel == "noShields") { //play the phaserSound sound created in line 22 above phaserSound.play() //create a new instance of the phaser class and name it newPhaser var newPhaser:phaser = new phaser(); //set the x of the newPhaser to the x of the enterprise + Math.cos(radians) * 58; newPhaser.x = enterprise.x + Math.cos(radians) * 58; //set the y of the newPhaser to the y of the enterprise + Math.sin(radians) * 58; newPhaser.y = enterprise.y + Math.sin(radians) * 58; //set xmov property of the newPhaser to the correct value using Math.cos and phaserSpeed newPhaser.xmov = Math.cos(radians) * phaserSpeed; //set the ymov property of the newPhaser to the correct value using Math.sin and phaserSpeed newPhaser.ymov = Math.sin(radians) * phaserSpeed; //add and event listener to the newPhaser that listens for ENTER_FRAME and calls the movePhaser function newPhaser.addEventListener(Event.ENTER_FRAME,movePhaser); //using the addChild method add the newPhaser to the phaserContainer movie clip phaserContainer.addChild(newPhaser); } } } function movePhaser(evt:Event):void { //move the phaser along the x axis using the evt.target reference and xmov property evt.target.x += evt.target.xmov; //move the phaser along the y axis using the evt.target reference and ymov property evt.target.y += evt.target.ymov; //in the if below check for a collision between the evt.target and the birdOfPrey movie clips using hitTestObject if(evt.target.hitTestObject(birdOfPrey)) { evt.target.removeEventListener(Event.ENTER_FRAME, movePhaser); phaserContainer.removeChild(MovieClip(evt.target)); setBirdOfPrey(); clearPhasers(); score += Math.ceil(birdOfPreySpeed * 10); updateScore(); } else if(evt.target.x < 0 || evt.target.x > stage.stageWidth || evt.target.y < 0 || evt.target.y > stage.stageHeight) { evt.target.removeEventListener(Event.ENTER_FRAME, movePhaser); phaserContainer.removeChild(MovieClip(evt.target)); } } function moveBirdOfPrey(evt:Event):void { birdOfPrey.x += birdOfPrey.xmov; birdOfPrey.y += birdOfPrey.ymov; //in the if below check for a collision between the birdOfPrey and the topSide movie clips then use the or || operator to check //for a collision between the birdOfPrey and the bottomSide movie clips if((birdOfPrey.hitTestObject(topSide)) || (birdOfPrey.hitTestObject(bottomSide))) { birdOfPrey.ymov *= -1; birdOfPrey.rotation *= -1; } //in the if below check for a collision between the birdOfPrey and the leftSide movie clips then use the or || operator to check //for a collision between the birdOfPrey and the rightSide movie clips if((birdOfPrey.hitTestObject(leftSide)) || (birdOfPrey.hitTestObject(rightSide))) { birdOfPrey.xmov *= -1; birdOfPrey.rotation = 180 - birdOfPrey.rotation; } //in the if below check for a collision between the birdOfPrey and the enterprise movie clips //and then use the && and operator to check for the currentLabel of the enterprise to be equal to "shields" //enterprise.currentLabel == "shields" if((birdOfPrey.hitTestObject(enterprise)) && (enterprise.currentLabel == "shields")) { clearPhasers(); setBirdOfPrey(); } //in the if below check for a collision between the birdOfPrey and the enterprise movie clips if(birdOfPrey.hitTestObject(enterprise)) { if(livesContainer.numChildren > 0) { livesContainer.removeChildAt(0); score -= 5; updateScore(); clearPhasers(); setBirdOfPrey(); if(livesContainer.numChildren == 0) { endGame(); } } } } function endGame() { stage.removeEventListener(Event.ENTER_FRAME, moveBirdOfPrey); stage.removeEventListener(MouseEvent.MOUSE_MOVE, aimEnterprise); stage.removeEventListener(MouseEvent.CLICK, firePhaser); stage.removeEventListener(KeyboardEvent.KEY_DOWN, shieldOn); stage.removeEventListener(KeyboardEvent.KEY_UP, shieldOff); restartGame.visible = true; } function updateScore() { scoreAmount.text = "Score: " + formatNumber(score); } function setBirdOfPrey() { birdOfPreySpeed = randomRange(3, 25); birdOfPreyDegrees = Math.random() * 360; birdOfPreyRadians = birdOfPreyDegrees * Math.PI / 180; birdOfPrey.xmov = Math.cos(birdOfPreyRadians) * birdOfPreySpeed; birdOfPrey.ymov = Math.sin(birdOfPreyRadians) * birdOfPreySpeed; birdOfPrey.rotation = birdOfPreyDegrees; randomStart = randomRange(0,3); birdOfPrey.x = startingPoints[randomStart][0]; birdOfPrey.y = startingPoints[randomStart][1] speedIndicator.speedMask.width = birdOfPreySpeed * 4; speedIndicator.speedPercent.text = String(Math.floor((speedIndicator.speedMask.width / 100) * 100)) + "%"; } function randomRange(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function clearPhasers() { SoundMixer.stopAll(); while(phaserContainer.numChildren != 0) { var tempPhaser = phaserContainer.getChildAt(0); tempPhaser.removeEventListener(Event.ENTER_FRAME, movePhaser); phaserContainer.removeChildAt(0); } } function createLives() { for(var l = 1; l < 4; l++) { var newLife:life = new life(); newLife.x = 50 + (l * 65); newLife.y = 25; livesContainer.addChild(newLife); } } function formatNumber(incNumber) { var sIncNumber = String(incNumber); var commaCounter:int = 1; var tempNString = ""; for(var n = sIncNumber.length - 1; n >= 0; n--) { tempNString = sIncNumber.substr(n, 1) + tempNString; if(commaCounter % 3 == 0) { tempNString = "," + tempNString; } commaCounter ++; } if(tempNString.substr(0, 1) == ",") { tempNString = tempNString.substr(1, tempNString.length); } return tempNString; } createLives(); updateScore(); setBirdOfPrey();