/src/Main.as

https://github.com/marlonhs/PhysicsCannonGameSource
ActionScript | 154 lines | 110 code | 32 blank | 12 comment | 2 complexity | 154392ccb741d0aed38e0d79792bfd6a MD5 | raw file
  1. package
  2. {
  3. import events.GameEvent;
  4. import flash.display.MovieClip;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.events.MouseEvent;
  8. import flash.media.SoundTransform;
  9. import flash.system.ApplicationDomain;
  10. import game.GameLogic;
  11. import game.HUD;
  12. import game.PhysicsGame;
  13. import game.SoundManager;
  14. import screens.GameCompleteScreen;
  15. import screens.HelpScreen;
  16. import screens.IntroAnimation;
  17. import screens.PauseScreen;
  18. import screens.StartScreen;
  19. /**
  20. * Main class handles Application Logic.
  21. * It does not handle game logic. that is handled by the game class.
  22. * It manages the adding and removal of screens, and communicates with the screens and game.
  23. * It instantiates the game, and dispatches events to start, end, restart and pause the game.
  24. * It instantiates the sound manager, and dispatches events to start, end, restart and pause sounds.
  25. * It exposes graphics and sounds to the game and UI classes.
  26. * it recives and routes UI events to and from the game and sound manager.
  27. * @author DefaultUser (Tools -> Custom Arguments...)
  28. */
  29. public class Main extends Sprite
  30. {
  31. //Screens
  32. private var introAnimation:Sprite
  33. private var startScreen:Sprite;
  34. private var helpScreen:Sprite;
  35. private var gameCompleteScreen:GameCompleteScreen;
  36. private var pauseScreen:Sprite;
  37. private var gameSprite:GameLogic
  38. public var _mochiads_game_id:String = "YOUR MOCHI ID HERE";
  39. public static var APP_DOM:ApplicationDomain;
  40. public function Main():void
  41. {
  42. if (stage) init();
  43. else addEventListener(Event.ADDED_TO_STAGE, init);
  44. }
  45. private function init(e:Event = null):void
  46. {
  47. removeEventListener(Event.ADDED_TO_STAGE, init);
  48. APP_DOM = ApplicationDomain.currentDomain;
  49. createScreens();
  50. playIntroAnimation();
  51. }
  52. private function createScreens():void
  53. {
  54. introAnimation = new IntroAnimation();
  55. introAnimation.addEventListener("introComplete", onIntroComplete)
  56. startScreen = new StartScreen()
  57. helpScreen = new HelpScreen();
  58. helpScreen.visible = false;
  59. gameCompleteScreen = new GameCompleteScreen();
  60. gameCompleteScreen.visible = false;
  61. pauseScreen = new PauseScreen();
  62. pauseScreen.visible = false;
  63. gameSprite = new GameLogic();
  64. addChild(gameSprite);
  65. addChild(startScreen);
  66. addChild(introAnimation);
  67. addChild(gameCompleteScreen);
  68. addChild(pauseScreen);
  69. addChild(helpScreen);
  70. this.addEventListener(GameEvent.GAME_COMPLETE, showGameCompleteScreen)
  71. this.addEventListener(GameEvent.CLOSE_SCREEN, closeTargetScreen)
  72. this.addEventListener(GameEvent.START_GAME, startNewGame);
  73. this.addEventListener(GameEvent.SHOW_HELP_SCREEN, showHelpScreen);
  74. this.addEventListener(GameEvent.PAUSE_GAME, pauseGame);
  75. this.addEventListener(GameEvent.RESUME_GAME, resumeGame);
  76. }
  77. private function showGameCompleteScreen(e:GameEvent):void
  78. {
  79. trace("showGameCompleteScreen", "MAIN");
  80. gameCompleteScreen.visible = true;
  81. gameCompleteScreen.setScore(gameSprite.score);
  82. }
  83. private function resumeGame(e:GameEvent):void
  84. {
  85. trace("Main.resumeGame", "MAIN");
  86. pauseScreen.visible = false;
  87. gameSprite.resumeGame();
  88. }
  89. private function pauseGame(e:GameEvent):void
  90. {
  91. trace("Main.PauseGame", "MAIN");
  92. pauseScreen.visible = true;
  93. gameSprite.pauseGame();
  94. }
  95. private function closeTargetScreen(e:GameEvent):void
  96. {
  97. e.target.visible = false;
  98. }
  99. private function hideHelpScreen(e:GameEvent):void
  100. {
  101. helpScreen.visible = false;
  102. }
  103. private function showHelpScreen(e:GameEvent):void
  104. {
  105. trace("SHOW HELP SCREEN", "MAIN");
  106. helpScreen.visible = true;
  107. }
  108. private function startNewGame(e:GameEvent):void
  109. {
  110. gameSprite.startNewGame();
  111. }
  112. private function onIntroComplete(e:Event):void
  113. {
  114. introAnimation.visible = false;
  115. }
  116. private function playIntroAnimation():void
  117. {
  118. //TODO May be encessary to tell introAnimation to play if you want it to play twice.
  119. introAnimation.visible = true;
  120. }
  121. }
  122. }