Advertisement
Guest User

911 was kek

a guest
Dec 19th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. // Load in Library functions
  2. import flash.events.KeyboardEvent;
  3. import flash.events.Event;
  4. import flash.media.Sound;
  5. import flash.events.MouseEvent;
  6.  
  7. // Event listeners wait to be triggered before running functions
  8. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
  9. stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
  10. stage.addEventListener(Event.ENTER_FRAME, fEnterFrame);
  11.  
  12. // Global variables that can be used by all of the functions within the script
  13. // Keeps track of whether or not an arrow key is being pressed
  14. var up:Boolean = false;
  15. var down:Boolean = false;
  16. var left:Boolean = false;
  17. var right:Boolean = false;
  18. var spaceBar:Boolean = false;
  19.  
  20. var speed:int = 8; //The speed at which the ship will move each time a key is pressed
  21. var haySpeed:int = 10; //how fast the hays move
  22. var hays:Array = []; //Keeps track of hays on screen
  23.  
  24. var enemies:Array = []; //Keep track of all enemies on screen
  25. // Checks which key was pressed by the users and sets the coresponding variable
  26. var score:int = 0;
  27. var lives:int = 3;
  28.  
  29. ScoreBoardDisplay.txtscore.text = String(score);
  30. ScoreBoardDisplay.txtlives.text = String(lives);
  31.  
  32. //Make the end game symbol invisible
  33. EndGame.visible = false;
  34.  
  35. //Intialise choices of chracter for the player
  36. var myFarmerboy:Farmerboy = new Farmerboy();
  37. var myCharacter = new Farmer();
  38. var characterSelected:Boolean = false;
  39.  
  40. function keyDownHandler (event:KeyboardEvent):void
  41. {
  42. switch(event.keyCode) //Check whcih key it was
  43. {
  44. case Keyboard.UP:
  45. up = true; //Set global variable for this key
  46. break; // Move on to check if any other keys
  47. case Keyboard.DOWN:
  48. down = true;
  49. break;
  50. case Keyboard.LEFT:
  51. left = true;
  52. break;
  53. case Keyboard.RIGHT:
  54. right = true;
  55. break;
  56. case Keyboard.SPACE:
  57. spaceBar = true;
  58. break;
  59.  
  60. case 49: //if 1 key was pressed
  61. {
  62. //hide intructions symbol
  63. myInstructions.visible = false;
  64. this.myFarmer.addChild(myCharacter);
  65. characterSelected = true; //Ship selected
  66. break;
  67. }
  68. case 50: //if 2 key was pressed
  69. {
  70. //hide instructions symbol
  71. myInstructions.visible = false;
  72. this.myFarmer.addChild(myFarmerboy);
  73. characterSelected = true; //chracter selected
  74. break;
  75. }
  76. }
  77. }
  78.  
  79. function keyUpHandler (event:KeyboardEvent):void
  80. {
  81. switch(event.keyCode) // Check which key it was
  82. {
  83. case Keyboard.UP:
  84. up = false; //Set a global variable for this key
  85. break; //move on to the check if any other keys have also been released
  86. case Keyboard.DOWN:
  87. down = false;
  88. break;
  89. case Keyboard.LEFT:
  90. left = false;
  91. break;
  92. case Keyboard.RIGHT:
  93. right = false;
  94. break;
  95. case Keyboard.SPACE:
  96. spaceBar = false;
  97. break;
  98. }
  99. }
  100.  
  101. // Checks Global variables to see which keys are being pressed and moves the ship accordingly
  102. function fEnterFrame(event:Event):void
  103. {
  104. //if left is pressed but not right and the ship is on stage
  105. if(left && !right && myFarmer.x >0) {
  106. myFarmer.x -= speed;
  107. }
  108.  
  109. //If right is pressed and left is not and the ship is on stage
  110. {
  111. if(right && !left && myFarmer.x < stage.stageWidth) {
  112. myFarmer.x += speed;
  113. }
  114. }
  115.  
  116. //if up is pressed and down is not and the ship is on the stag
  117. {
  118. if(up && !down && myFarmer.y >0) {
  119. myFarmer.y -=speed;
  120. }
  121. }
  122.  
  123. //if down is pressed and up is not and ship is on stage
  124. if(down && !up && myFarmer.y < stage.stageHeight) {
  125. myFarmer.y += speed;
  126. }
  127.  
  128.  
  129. //Fire hay
  130. if(spaceBar) {
  131. addhay(myFarmer.x, myFarmer.y); //makes hay appear at the front
  132. }
  133.  
  134. hayAction(); //Calls a function that controls the hay
  135.  
  136. enemyAction(); //Calls a function that controls the enemies
  137.  
  138. enemyKilled(); //calls a function that deals with the player shooting the enemy
  139. }
  140.  
  141. function addhay (startX, startY):void
  142. {
  143. //Only add a hay if the maximum number are not already on stage
  144. if(hays.length <5)
  145. {
  146. //Declare a new instance from the hay class
  147. var b:hay = new hay();
  148.  
  149. //set the starting position of the hay to match the front of th ship
  150. b.x = startX;
  151. b.y = startY;
  152.  
  153. stage.addChild(b); //Add the new hay to the stage
  154.  
  155. hays.push(b); //store the hay in the hay array
  156. }
  157. }
  158.  
  159. function hayAction():void
  160. {
  161. //loop through all the instances of the hay
  162. // loop from 0 to the number of hays - 1
  163. for (var i:int = 0; i<= hays.length - 1; i++)
  164. {
  165. hays[i].x += haySpeed; //move the hay
  166.  
  167. // if the hay flies off the screen, remove it
  168. if(hays[i].x > stage.stageWidth)
  169. {
  170. stage.removeChild(hays[i]); //remove from stage
  171.  
  172. hays.splice(i, 1); //remove from the array
  173. }
  174. }
  175. }
  176.  
  177. function enemyAction():void
  178. {
  179. //Limit the number of enemies on screen at once
  180. if(enemies.length < 4 && characterSelected == true)
  181. {
  182. //Declare an instance of enemy from the enemy class
  183. var e:Enemy = new Enemy();
  184.  
  185. // Set the left edge of the enemy to the far right edge of the stage
  186. var startX: int = stage.stageWidth;
  187.  
  188. // randomly choose the vertical starting point of enemy
  189. var startY:int = Math.random() * stage.stageHeight;
  190.  
  191. //Assign the starting coordinate to the new hay
  192. e.x = startX;
  193. e.y = startY;
  194.  
  195. //Display the new hay on stage
  196. stage.addChild(e);
  197.  
  198. //Store the new hay in the array
  199. enemies.push(e);
  200. }
  201.  
  202. //loop through each existing enemy
  203. //decade where it should be and what should happen to it
  204. for (var i:int = 0; i <= enemies.length - 1; i++)
  205. {
  206. //if the enemy is above the player's ship then move the enemy down
  207. if(enemies[i].y > myFarmer.y) {
  208. enemies[i].y -=2
  209. //otherwise move the enemy up towards the player ship
  210. }else if(enemies[i].y < myFarmer.y){
  211. enemies[i].y += 2
  212. }
  213.  
  214. //if the enemy is already level with the players ship just move it sideways
  215. enemies[i].x -= 10;
  216.  
  217. //if the enemy flies off the screen, remove it
  218. if(enemies[i].x < 0)
  219. {
  220. //Remove the enemy from the stage
  221. stage.removeChild(enemies[i]);
  222.  
  223. //remove the enemy from the array
  224. enemies.splice (i, 1);
  225. }
  226.  
  227. //if an enemy hits the players ship
  228. if(enemies[i].hitTestObject(myFarmer)){
  229. //remove the enemy from the stage
  230. stage.removeChild(enemies[i]);
  231.  
  232. //remove the enemy from the array of existing enemies
  233. enemies.splice(i, 1);
  234.  
  235. lives -=1; //Take one life from then player
  236. ScoreBoardDisplay.txtlives.text = String(lives);
  237. //if the players lives have all been used
  238. if(lives == 0){
  239. //Remove event listeners so that the movement keys no longer respond
  240. stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
  241. stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
  242.  
  243. stage.removeEventListener(Event.ENTER_FRAME,fEnterFrame);
  244.  
  245. //remove hays from screen as they would appear in front of the end game symbol
  246. for(var j:int = 0; j<= hays.length - 1; j++){
  247. stage.removeChild(hays[j]);
  248. }
  249.  
  250. //remove enemies from the screen as they would appear in front of the end game symbol
  251. for(var k:int = 0; k<= enemies.length - 1; k++){
  252. stage.removeChild(enemies[k]);
  253. }
  254.  
  255. //store the final score
  256. EndGame.txtFinalScore.text = String(score);
  257.  
  258. //Display finalscore end game screen
  259. EndGame.visible = true;
  260. }
  261. }
  262. }
  263. }
  264.  
  265. function enemyKilled():void
  266. {
  267. //Loop through all of the hays on screen to see what should happen to them
  268. for (var i:int = 0; i <= hays.length - 1; i++)
  269. {
  270. //Check through all of the enemies to see if this particular hay has hit one
  271. for (var j:int = 0; j <= enemies.length - 1;j++)
  272. {
  273. // if a hay has hit an enemy
  274. if (hays[i].hitTestObject(enemies[j])){
  275. //remove the hay and the enemy from the stage
  276. stage.removeChild(enemies[j]);
  277. stage.removeChild(hays[i]);
  278.  
  279. // remove the hay and enemy from their arrays
  280. enemies.splice(j,1);
  281. hays.splice(i,1);
  282. score += 1;
  283. ScoreBoardDisplay.txtscore.text = String(score);
  284. }
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement