import fl.transitions.Tween; import fl.transitions.easing.*; import fl.transitions.TweenEvent; import flash.media.Sound; import flash.net.SharedObject; /**************VARIABLES**************/ var STATE_INIT_GAME:String = "STATE_INIT_GAME"; var STATE_START_PLAYER:String = "STATE_START_PLAYER"; var STATE_PLAY_GAME:String = "STATE_PLAY_GAME"; var STATE_END_GAME:String = "STATE_END_GAME"; var gameState:String; var player:MovieClip; var enemies:Array; var level:Number; var score:Number; var lives:Number; var accel:Accelerometer; var Lasers:Array; var explosions:Array; var hiddenOptions:Boolean = true; /**************SETUP**************/ endScreen.visible = false; optionsMenu.visible = false; /**************INTRO SCREEN**************/ introScreen.play_btn.addEventListener(MouseEvent.CLICK, clickAway); function clickAway(event:MouseEvent):void { moveScreenOff(introScreen); } //Gesture Swipe, zoom, rotate, pan Multitouch.inputMode = MultitouchInputMode.GESTURE; introScreen.addEventListener(TransformGestureEvent.GESTURE_SWIPE, swipeAway); function swipeAway(event:TransformGestureEvent):void { //Swipe Left (-1) Right(1), offsetY(-1), offsetY(1) down. if (event.offsetX == -1) { //Do something moveScreenOff(introScreen); } } function moveScreenOff(screen:MovieClip):void { //Move the screen off... var introTween = new Tween(screen,"x",Strong.easeInOut,screen.x,(screen.width)*-1,1,true); //When the motion has finished... introTween.addEventListener(TweenEvent.MOTION_FINISH, tweenFinish); function tweenFinish(e:TweenEvent):void { trace("tweenFinish"); //Establish the game state... gameState = STATE_INIT_GAME; trace(gameState); //Fire off the gameLoop function at the frame rate of the movie... addEventListener(Event.ENTER_FRAME, gameLoop); } } /**************GAME STATES**************/ function gameLoop(e:Event):void { //Keep track of the gameState switch (gameState) { case STATE_INIT_GAME : initGame(); break; case STATE_START_PLAYER : startPlayer(); break; case STATE_PLAY_GAME : playGame(); break; case STATE_END_GAME : endGame(); break; } } /**************STATE_INIT_GAME**************/ function initGame():void { level = 1; level_txt.text = String(level); score = 0; score_txt.text = String(score); lives = 2; lives_txt.text = String(lives); //Set up all graphics... //Pull in the Player from the library... player = new Player(); //Create an enemies array (a big box that can hold many things) enemies = new Array(); //Create an array to hold all lasers Lasers = new Array(); //Create an array called explosions explosions = new Array(); gameState = STATE_START_PLAYER; trace(gameState); } /**************STATE_START_PLAYER**************/ function startPlayer():void { //Create the player, properties... player.y = stage.stageHeight - player.height; player.cacheAsBitmap = true; addChild(player); //Start firing the laser laserTimer.start(); //Create an accelerometer; accel = new Accelerometer(); //If the phone has accelerometer support... if (Accelerometer.isSupported) { //As the user tilts their device... accel.addEventListener(AccelerometerEvent.UPDATE, accelMove); } else { //If there is no accelerometer support... addEventListener(Event.ENTER_FRAME, movePlayer); } gameState = STATE_PLAY_GAME; trace(gameState); } function accelMove(event:AccelerometerEvent):void { //accelerationX returns a number between 0,1. Properties: Y, Z. player.x -= event.accelerationX * 80; if (player.x < 0) { player.x = 0; } else if (player.x > (stage.stageWidth - player.width) ) { player.x = stage.stageWidth - player.width; } } function movePlayer(e:Event):void { player.x = stage.mouseX; //Doesn't go off the right or left side... if (player.x < 0) { player.x = 0; } else if (player.x > (stage.stageWidth - player.width) ) { player.x = stage.stageWidth - player.width; } } //Create a timer that executes a function every 1/2 second var laserTimer:Timer = new Timer(500); laserTimer.addEventListener(TimerEvent.TIMER, timerListener); function timerListener(e:TimerEvent):void { //In the Library linkage for a movieclip should be Laser var tempLaser:MovieClip = new Laser(); //Place the laser at the tip of the player's ship tempLaser.x = player.x +(player.width/2); tempLaser.y = player.y; tempLaser.cacheAsBitmap = true; tempLaser.speed = 10; Lasers.push(tempLaser); addChild(tempLaser); } //Lasers array is required function moveLaser():void { //Move Laser var tempLaser:MovieClip; for (var i=Lasers.length-1; i>=0; i--) { tempLaser = Lasers[i]; //Move the laser up based on the speed defined... tempLaser.y -= tempLaser.speed; if (tempLaser.y < 0) { //Remove laser 1 from Lasers array removeLaser(i); } } //Remove any tempExplosion from the explosions array that has already played var tempExplosion:MovieClip; for (i=explosions.length-1; i>=0; i--) { tempExplosion = explosions[i]; if (tempExplosion.currentFrame >= tempExplosion.totalFrames) { removeExplosion(i); } } } /**************STATE_PLAY_GAME**************/ function playGame():void { //Set up the enemies, explosions etc. makeEnemies(); moveEnemies(); moveLaser(); testForEnd(); } //Call this function for how many enemies you want to make... function makeEnemies():void { //Pick a number between 0-60 var chance:Number = Math.floor(Math.random() * 60); //If the number is less than or equal to 1+level (2+) if (chance <= 1 + level) { var tempEnemy:MovieClip; //Make sure a Library item linkage is set to Enemy... tempEnemy = new Enemy(); tempEnemy.speed = 3; //Math.random(); gets a random number from 0.0-1.0 tempEnemy.x = Math.round(Math.random() * 800); tempEnemy.cacheAsBitmapMatrix = tempEnemy.transform.concatenatedMatrix; tempEnemy.cacheAsBitmap = true; trace("tempEnemy"); addChild(tempEnemy); enemies.push(tempEnemy); } } //Create an enemies array if you do not already have one... //Create tempEnemy from Enemy library item if you do not already have one... function moveEnemies():void { var tempEnemy:MovieClip; for (var i:int =enemies.length-1; i>=0; i--) { tempEnemy = enemies[i]; //rotate the enemy between 10-5 degrees tempEnemy.rotation += (Math.round(Math.random()*10-5)); //Find the rotation and move the x position that direction tempEnemy.x -= (Math.sin((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed; tempEnemy.y += (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed; if (tempEnemy.x < 0) { tempEnemy.x = 0; } if (tempEnemy.x > stage.stageWidth) { tempEnemy.x = stage.stageWidth; } if (tempEnemy.y > stage.stageHeight || tempEnemy.hitTestObject(player)) { makeExplosion(tempEnemy.x, tempEnemy.y); //Remove enemy from enemies array removeEnemy(i); //Subtract a life lives--; lives_txt.text = String(lives); } //Make Explosion... var tempLaser:MovieClip; tempEnemy = enemies[i]; //Loop through the Lasers array for (var j:int=Lasers.length-1; j>=0; j--) { tempLaser = Lasers[j]; if (tempEnemy.hitTestObject(tempLaser)) { makeExplosion(tempEnemy.x, tempEnemy.y); //Remove enemy from enemies array removeEnemy(i); //Remove laser from Lasers array removeLaser(j); //Add to the score score++; score_txt.text = String(score); } } } } function makeExplosion(ex:Number, ey:Number):void { var tempExplosion:MovieClip; tempExplosion = new Explosion(); tempExplosion.x = ex; tempExplosion.y = ey; addChild(tempExplosion); explosions.push(tempExplosion); //Play an explosion sound; var sound:Sound = new Explode(); sound.play(); } function testForEnd():void { // if (score > level * 10) { level++; level_txt.text = String(level); } //Check to see how many lives the user has if (lives == 0) { gameState = STATE_END_GAME; trace(gameState); } } function removeEnemy(idx:int) { removeChild(enemies[idx]); enemies.splice(idx,1); } function removeLaser(idx:int) { removeChild(Lasers[idx]); Lasers.splice(idx,1); } function removeExplosion(idx:int) { removeChild(explosions[idx]); explosions.splice(idx,1); } /**************END SCREEN**************/ function endGame():void { removeGame(); endScreen.visible = true; removeEventListener(Event.ENTER_FRAME, gameLoop); showResults(); } /**************REMOVE GAME**************/ function removeGame():void { trace("Remove Game"); //Loop through an enemies array for (var i:int = enemies.length-1; i >=0; i--) { removeEnemy(i); } //Remove lasers for (var j:int = Lasers.length-1; j >=0; j--) { removeLaser(j); } //Explosions for (var k:int = explosions.length-1; k >=0; k--) { removeExplosion(k); } //Remove the player removeChild(player); laserTimer.stop(); } function showResults():void { trace("Show Results"); //Hides the text field and enter button... endScreen.enter_btn.visible = false; endScreen.highScoreName.visible = false; //Create a shared object that will store the all time high score var so:SharedObject = SharedObject.getLocal("alltimeHighScore"); //Show results //If the score is greater than zero if (so.data.score == undefined || score > so.data.score) { endScreen.highScore.text = "You made it to level " + level + " with a high score of " + score + ". \n Enter your name below."; //Turns on the enter button and the input text field.. endScreen.enter_btn.visible = true; endScreen.highScoreName.visible = true; } else { //If the player didn't get the high score... endScreen.highScore.text = "Your score of " + score + " does not beat " + so.data.score + " by " + so.data.name + "."; } //Turning on and making the enter button "work" endScreen.enter_btn.addEventListener(MouseEvent.CLICK, clickEnter); function clickEnter(event:MouseEvent):void { trace("clickEnter"); endScreen.highScore.text = "Great job, " + endScreen.highScoreName.text + "! \n You made it to " + level + " with a score of " + score + "!"; //Define the properties of the shared object (so) so.data.score = score; so.data.level = level; so.data.name = endScreen.highScoreName.text; so.flush(); //Hides the enter button just selected and the input text field... endScreen.enter_btn.visible = false; endScreen.highScoreName.visible = false; } //Enables the play button endScreen.play_btn.addEventListener(MouseEvent.CLICK, clickFinalAway); function clickFinalAway(event:MouseEvent):void { trace("clickFinalAway"); moveScreenOff(endScreen); } } /**************OPTIONS MENU**************/ //If any hardware key is pressed... stage.addEventListener(KeyboardEvent.KEY_UP, optionsKey); function optionsKey(event:KeyboardEvent):void { //options key/menu 95 //The enter key is 13 if (event.keyCode == 95 || event.keyCode == 13) { //If it's set to true... if (hiddenOptions) { //Put the optionsMenu on top of everything... setChildIndex(optionsMenu,numChildren-1); optionsMenu.visible = true; optionsMenu.addEventListener(MouseEvent.CLICK, exitApp); pauseGame(); } else { //If the optionsMenu is showing then hide it... optionsMenu.visible = false; optionsMenu.removeEventListener(MouseEvent.CLICK, exitApp); resumeGame(); } //Toggle... hiddenOptions = ! hiddenOptions; } } function exitApp(event:MouseEvent):void { trace("exitApp"); //UNCOMMENT ON PUBLISH //AIR functionality //NativeApplication.nativeApplication.exit(0); } //UNCOMMENT ON PUBLISH - SET THE NECESSARY PERMISSIONS... //AIR for Android devices - Keep screen awake if you are using the Accelerometer etc. //NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE; //When the user gets a phone call, or launches another app... stage.addEventListener(Event.DEACTIVATE, Deactivate); function Deactivate(event:Event):void { pauseGame(); } //When the user comes back to the game... stage.addEventListener(Event.ACTIVATE, Activate); function Activate(event:Event):void { resumeGame(); } function pauseGame():void { //Remove any listener events, timers etc. if (gameState == STATE_PLAY_GAME) { removeEventListener(Event.ENTER_FRAME, gameLoop); laserTimer.stop(); accel.removeEventListener(AccelerometerEvent.UPDATE, accelMove); } } function resumeGame():void { //Activate any listener events, timers etc. if (gameState == STATE_PLAY_GAME) { addEventListener(Event.ENTER_FRAME, gameLoop); laserTimer.start(); accel.addEventListener(AccelerometerEvent.UPDATE, accelMove); } }