Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load in Library functions
- import flash.events.KeyboardEvent;
- import flash.events.Event;
- import flash.media.Sound;
- import flash.events.MouseEvent;
- // Event listeners wait to be triggered before running functions
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
- stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
- stage.addEventListener(Event.ENTER_FRAME, fEnterFrame);
- // Global variables that can be used by all of the functions within the script
- // Keeps track of whether or not an arrow key is being pressed
- var up:Boolean = false;
- var down:Boolean = false;
- var left:Boolean = false;
- var right:Boolean = false;
- var spaceBar:Boolean = false;
- var speed:int = 8; //The speed at which the ship will move each time a key is pressed
- var haySpeed:int = 10; //how fast the hays move
- var hays:Array = []; //Keeps track of hays on screen
- var enemies:Array = []; //Keep track of all enemies on screen
- // Checks which key was pressed by the users and sets the coresponding variable
- var score:int = 0;
- var lives:int = 3;
- ScoreBoardDisplay.txtscore.text = String(score);
- ScoreBoardDisplay.txtlives.text = String(lives);
- //Make the end game symbol invisible
- EndGame.visible = false;
- //Intialise choices of chracter for the player
- var myFarmerboy:Farmerboy = new Farmerboy();
- var myCharacter = new Farmer();
- var characterSelected:Boolean = false;
- function keyDownHandler (event:KeyboardEvent):void
- {
- switch(event.keyCode) //Check whcih key it was
- {
- case Keyboard.UP:
- up = true; //Set global variable for this key
- break; // Move on to check if any other keys
- case Keyboard.DOWN:
- down = true;
- break;
- case Keyboard.LEFT:
- left = true;
- break;
- case Keyboard.RIGHT:
- right = true;
- break;
- case Keyboard.SPACE:
- spaceBar = true;
- break;
- case 49: //if 1 key was pressed
- {
- //hide intructions symbol
- myInstructions.visible = false;
- this.myFarmer.addChild(myCharacter);
- characterSelected = true; //Ship selected
- break;
- }
- case 50: //if 2 key was pressed
- {
- //hide instructions symbol
- myInstructions.visible = false;
- this.myFarmer.addChild(myFarmerboy);
- characterSelected = true; //chracter selected
- break;
- }
- }
- }
- function keyUpHandler (event:KeyboardEvent):void
- {
- switch(event.keyCode) // Check which key it was
- {
- case Keyboard.UP:
- up = false; //Set a global variable for this key
- break; //move on to the check if any other keys have also been released
- case Keyboard.DOWN:
- down = false;
- break;
- case Keyboard.LEFT:
- left = false;
- break;
- case Keyboard.RIGHT:
- right = false;
- break;
- case Keyboard.SPACE:
- spaceBar = false;
- break;
- }
- }
- // Checks Global variables to see which keys are being pressed and moves the ship accordingly
- function fEnterFrame(event:Event):void
- {
- //if left is pressed but not right and the ship is on stage
- if(left && !right && myFarmer.x >0) {
- myFarmer.x -= speed;
- }
- //If right is pressed and left is not and the ship is on stage
- {
- if(right && !left && myFarmer.x < stage.stageWidth) {
- myFarmer.x += speed;
- }
- }
- //if up is pressed and down is not and the ship is on the stag
- {
- if(up && !down && myFarmer.y >0) {
- myFarmer.y -=speed;
- }
- }
- //if down is pressed and up is not and ship is on stage
- if(down && !up && myFarmer.y < stage.stageHeight) {
- myFarmer.y += speed;
- }
- //Fire hay
- if(spaceBar) {
- addhay(myFarmer.x, myFarmer.y); //makes hay appear at the front
- }
- hayAction(); //Calls a function that controls the hay
- enemyAction(); //Calls a function that controls the enemies
- enemyKilled(); //calls a function that deals with the player shooting the enemy
- }
- function addhay (startX, startY):void
- {
- //Only add a hay if the maximum number are not already on stage
- if(hays.length <5)
- {
- //Declare a new instance from the hay class
- var b:hay = new hay();
- //set the starting position of the hay to match the front of th ship
- b.x = startX;
- b.y = startY;
- stage.addChild(b); //Add the new hay to the stage
- hays.push(b); //store the hay in the hay array
- }
- }
- function hayAction():void
- {
- //loop through all the instances of the hay
- // loop from 0 to the number of hays - 1
- for (var i:int = 0; i<= hays.length - 1; i++)
- {
- hays[i].x += haySpeed; //move the hay
- // if the hay flies off the screen, remove it
- if(hays[i].x > stage.stageWidth)
- {
- stage.removeChild(hays[i]); //remove from stage
- hays.splice(i, 1); //remove from the array
- }
- }
- }
- function enemyAction():void
- {
- //Limit the number of enemies on screen at once
- if(enemies.length < 4 && characterSelected == true)
- {
- //Declare an instance of enemy from the enemy class
- var e:Enemy = new Enemy();
- // Set the left edge of the enemy to the far right edge of the stage
- var startX: int = stage.stageWidth;
- // randomly choose the vertical starting point of enemy
- var startY:int = Math.random() * stage.stageHeight;
- //Assign the starting coordinate to the new hay
- e.x = startX;
- e.y = startY;
- //Display the new hay on stage
- stage.addChild(e);
- //Store the new hay in the array
- enemies.push(e);
- }
- //loop through each existing enemy
- //decade where it should be and what should happen to it
- for (var i:int = 0; i <= enemies.length - 1; i++)
- {
- //if the enemy is above the player's ship then move the enemy down
- if(enemies[i].y > myFarmer.y) {
- enemies[i].y -=2
- //otherwise move the enemy up towards the player ship
- }else if(enemies[i].y < myFarmer.y){
- enemies[i].y += 2
- }
- //if the enemy is already level with the players ship just move it sideways
- enemies[i].x -= 10;
- //if the enemy flies off the screen, remove it
- if(enemies[i].x < 0)
- {
- //Remove the enemy from the stage
- stage.removeChild(enemies[i]);
- //remove the enemy from the array
- enemies.splice (i, 1);
- }
- //if an enemy hits the players ship
- if(enemies[i].hitTestObject(myFarmer)){
- //remove the enemy from the stage
- stage.removeChild(enemies[i]);
- //remove the enemy from the array of existing enemies
- enemies.splice(i, 1);
- lives -=1; //Take one life from then player
- ScoreBoardDisplay.txtlives.text = String(lives);
- //if the players lives have all been used
- if(lives == 0){
- //Remove event listeners so that the movement keys no longer respond
- stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
- stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
- stage.removeEventListener(Event.ENTER_FRAME,fEnterFrame);
- //remove hays from screen as they would appear in front of the end game symbol
- for(var j:int = 0; j<= hays.length - 1; j++){
- stage.removeChild(hays[j]);
- }
- //remove enemies from the screen as they would appear in front of the end game symbol
- for(var k:int = 0; k<= enemies.length - 1; k++){
- stage.removeChild(enemies[k]);
- }
- //store the final score
- EndGame.txtFinalScore.text = String(score);
- //Display finalscore end game screen
- EndGame.visible = true;
- }
- }
- }
- }
- function enemyKilled():void
- {
- //Loop through all of the hays on screen to see what should happen to them
- for (var i:int = 0; i <= hays.length - 1; i++)
- {
- //Check through all of the enemies to see if this particular hay has hit one
- for (var j:int = 0; j <= enemies.length - 1;j++)
- {
- // if a hay has hit an enemy
- if (hays[i].hitTestObject(enemies[j])){
- //remove the hay and the enemy from the stage
- stage.removeChild(enemies[j]);
- stage.removeChild(hays[i]);
- // remove the hay and enemy from their arrays
- enemies.splice(j,1);
- hays.splice(i,1);
- score += 1;
- ScoreBoardDisplay.txtscore.text = String(score);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement