Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.events.KeyboardEvent;
  5. import flash.events.Event;
  6. import com.game.Ship;
  7. import com.game.Bullet;
  8. import com.game.Asteroid;
  9. import flash.text.TextField;
  10. import flash.text.TextFormat;
  11.  
  12. public class Main extends MovieClip
  13. {
  14.  
  15. // score and character stuff
  16.  
  17. var playerScore:Number = 0;
  18. var score:TextField = new TextField();
  19. var scoreformat:TextFormat = new TextFormat();
  20.  
  21.  
  22.  
  23. // asteroid stuff
  24.  
  25. //how much time before another enemy is made
  26. var AsteroidTime:int = 0;
  27. //how much time needed to make an enemy
  28. //it should be more than the shooting rate
  29. //or else killing all of the enemies would
  30. //be impossible :O
  31. var AsteroidLimit:int = 16;
  32.  
  33.  
  34. // movement stuff
  35. public var isLeft:Boolean;
  36. public var isRight:Boolean;
  37. public var isUp:Boolean;
  38. public var isDown:Boolean;
  39.  
  40. // bullet stuff
  41.  
  42. //how much time before allowed to shoot again
  43. var cTime:int = 0;
  44. //the time it has to reach in order to be allowed to shoot (in frames)
  45. var cLimit:int = 12;
  46. //whether or not the user is allowed to shoot
  47. var shootAllow:Boolean = true;
  48.  
  49.  
  50.  
  51.  
  52. private var _ship:Ship;
  53.  
  54. public function Main()
  55. {
  56.  
  57. //this movieclip will hold all of the bullets
  58. var BulletContainer:MovieClip = new MovieClip();
  59. addChild(BulletContainer);
  60.  
  61.  
  62. // constructor code
  63. trace("hello");
  64.  
  65. stage.addEventListener(KeyboardEvent.KEY_DOWN, downHandler);
  66. stage.addEventListener(KeyboardEvent.KEY_UP, upHandler);
  67. addEventListener(Event.ENTER_FRAME, update);
  68.  
  69. createShip();
  70. createScoreField();
  71.  
  72.  
  73.  
  74.  
  75.  
  76. }
  77.  
  78. private function createScoreField()
  79. {
  80. score.text = "Score: " + playerScore;
  81. score.x = 20;
  82. score.y = 800;
  83. scoreformat.color = 0x00FF00;
  84. scoreformat.size = 40;
  85. score.setTextFormat( scoreformat );
  86.  
  87. stage.addChild(score);
  88.  
  89.  
  90. }
  91.  
  92.  
  93. private function createShip()
  94. {
  95. _ship = new Ship();
  96. _ship.setUp(350, 800, this);
  97.  
  98. addChild(_ship);
  99.  
  100. }
  101.  
  102. private function update(e:Event)
  103. {
  104.  
  105. if (cTime < cLimit)
  106. {
  107. cTime++;
  108. }
  109. else
  110. {
  111. shootAllow = true;
  112.  
  113. cTime = 0;
  114. }
  115.  
  116. //adding enemies to stage
  117. if (AsteroidTime < AsteroidLimit)
  118. {
  119. //if time hasn't reached the limit, then just increment
  120. AsteroidTime++;
  121. }
  122. else
  123. {
  124. //defining a variable which will hold the new enemy
  125. var newAsteroid = new Asteroid();
  126. //making the enemy offstage when it is created
  127. newAsteroid.y = -1 * newAsteroid.height;
  128. //making the enemy's x coordinates random
  129. //the "int" function will act the same as Math.floor but a bit faster
  130. newAsteroid.x = int(Math.random()*(stage.stageWidth - newAsteroid.width));
  131. //then add the enemy to stage
  132. addChild(newAsteroid);
  133. //and reset the enemyTime
  134. AsteroidTime = 0;
  135. }
  136. }
  137.  
  138.  
  139. private function upHandler(e:KeyboardEvent)
  140. {
  141. if (e.keyCode == 37)
  142. {
  143. isLeft = false;
  144. }
  145. else if (e.keyCode == 39)
  146. {
  147. isRight = false;
  148. }
  149. else if (e.keyCode == 38)
  150. {
  151. isUp = false;
  152. }
  153. else if (e.keyCode == 40)
  154. {
  155. isDown = false;
  156. }
  157. }
  158.  
  159. private function downHandler(e:KeyboardEvent)
  160. {
  161. if (e.keyCode == 37)
  162. {
  163. isLeft = true;
  164. }
  165. else if (e.keyCode == 39)
  166. {
  167. isRight = true;
  168. }
  169. else if (e.keyCode == 38)
  170. {
  171. isUp = true;
  172. }
  173. else if (e.keyCode == 40)
  174. {
  175. isDown = true;
  176. }
  177. else if (e.keyCode == 32 && shootAllow)
  178. {
  179. //making it so the user can't shoot for a bit
  180. shootAllow = false;
  181. //declaring a variable to be a new Bullet
  182. var newBullet:Bullet = new Bullet();
  183. //changing the bullet's coordinates
  184. newBullet.x = _ship.x + 17 - newBullet.width / 2;
  185. newBullet.y = _ship.y;
  186. //then we add the bullet to stage
  187. BulletContainer.addChild(newBullet);
  188.  
  189. }
  190. }
  191. }// end of class
  192. }// end of package
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement