/MusicGame/src/ui/UIInstrumentSelection.as

http://musicgamepbs.googlecode.com/ · ActionScript · 243 lines · 209 code · 34 blank · 0 comment · 6 complexity · b563b26c448c4ca863e8deedb003dd9d MD5 · raw file

  1. package ui
  2. {
  3. import animation.AnimationController;
  4. import animation.AnimationLibrary;
  5. import audio.AudioHandler;
  6. import audio.AudioLibrary;
  7. import entities.UIEntity;
  8. import flash.display.DisplayObject;
  9. import flash.display.MovieClip;
  10. import flash.display.SimpleButton;
  11. import flash.events.Event;
  12. import flash.events.MouseEvent;
  13. import flash.media.SoundTransform;
  14. import instruments.Guitar;
  15. import instruments.Instrument;
  16. import instruments.Piano;
  17. import instruments.Trombone;
  18. import instruments.Trumpet;
  19. import songs.Song;
  20. import songs.salsa.CalienteSwing;
  21. import utils.QuickFrameTimer;
  22. public class UIInstrumentSelection extends UIEntity
  23. {
  24. public static const INSTRUMENT_SELECTION_FRAME:int = 315;
  25. private var _graphic:SelectionUI;
  26. private var _song:Song;
  27. private var _instrument:Instrument;
  28. private var _instrumentButtons:Array;
  29. private var _songButtons:Array;
  30. private var _preLoader:ChuckVPreloader;
  31. public function UIInstrumentSelection()
  32. {
  33. _graphic = new SelectionUI();
  34. _instrumentButtons = new Array();
  35. _songButtons = new Array();
  36. addSongButtons();
  37. if (AnimationController.charactersAnimation is AnimationLibrary.getAnimationType(AnimationLibrary.CHARACTERS_INTRO))
  38. {
  39. }
  40. else
  41. {
  42. AnimationController.changeCharacterAnimation(AnimationLibrary.CHARACTERS_INTRO);
  43. }
  44. AnimationController.charactersAnimation.stop();
  45. AudioHandler.playSoundByLabels(AudioLibrary.CROWD_CHEERING, AudioLibrary.CROWD);
  46. AudioHandler.getSound(AudioLibrary.PICK_A_SONG).play();
  47. super(_graphic);
  48. }
  49. private function addSongButtons():void
  50. {
  51. var setList:Array = Level.genre.playlist;
  52. for (var i:int = 0; i < 4; i++)
  53. {
  54. var button:GSongButton = GSongButton(_graphic.setList.getChildByName("songButton" + (i + 1)));
  55. button.textField.mouseEnabled = false;
  56. if (setList.length <= i)
  57. {
  58. button.visible = false;
  59. continue;
  60. }
  61. var songButton:SongButton = new SongButton(button, setList[i]);
  62. songButton.addEventListener(MouseEvent.CLICK, selectSong);
  63. _songButtons.push(songButton);
  64. }
  65. }
  66. private function removeSongButtons():void
  67. {
  68. for (var i:int = 0; i < _songButtons.length; i++)
  69. {
  70. var songButton:SongButton = _songButtons[i];
  71. songButton.removeEventListener(MouseEvent.CLICK, selectSong);
  72. songButton.destroy();
  73. }
  74. }
  75. private function addInstrumentButtons():void
  76. {
  77. for (var i:int = 0; i < _song.instruments.length; i++)
  78. {
  79. var inst:Instrument = _song.instruments[i];
  80. var button:SimpleButton = SimpleButton(_graphic.getChildByName("instrumentButton" + (i + 1)));
  81. var graphic:InstrumentFrame = InstrumentFrame(_graphic.getChildByName("instrumentGraphic" + (i + 1)));
  82. graphic.addChildAt(AnimationLibrary.getAnimation(inst.buttonGraphic), 1);
  83. var instrumentButton:InstrumentButton = new InstrumentButton(button, inst);
  84. instrumentButton.addEventListener(MouseEvent.CLICK, clickedInstrumentButton);
  85. instrumentButton.addEventListener(MouseEvent.MOUSE_OVER, rollOverInstrumentButton);
  86. _instrumentButtons.push(instrumentButton);
  87. }
  88. }
  89. private function removeInstrumentButtons():void
  90. {
  91. for (var i:int = 0; i < _instrumentButtons.length; i++)
  92. {
  93. var button:InstrumentButton = _instrumentButtons[i];
  94. button.removeEventListener(MouseEvent.CLICK, clickedInstrumentButton);
  95. button.removeEventListener(MouseEvent.MOUSE_OVER, rollOverInstrumentButton);
  96. button.destroy();
  97. }
  98. }
  99. private function clickedSkip(event:MouseEvent):void
  100. {
  101. introAnimationComplete();
  102. }
  103. private function selectSong(event:MouseEvent):void
  104. {
  105. removeSongButtons();
  106. _song = SongButton(event.target).song;
  107. _song.loadAssets();
  108. _preLoader = new ChuckVPreloader(_song.loader.contentLoaderInfo);
  109. _preLoader.addEventListener(Event.COMPLETE, songLoaded);
  110. _graphic.addChild(_preLoader);
  111. }
  112. private function songLoaded(event:Event):void
  113. {
  114. _preLoader.removeEventListener(Event.COMPLETE, songLoaded);
  115. addInstrumentButtons();
  116. _graphic.play();
  117. introAnimationComplete();
  118. AnimationController.navigation.mainMenuButton.visible = true;
  119. }
  120. private function introAnimationComplete(event:Event = null):void
  121. {
  122. AnimationController.charactersAnimation.soundTransform = new SoundTransform(1);
  123. AnimationController.charactersAnimation.gotoAndPlay(Level.genre.instrumentFrame + 1);
  124. AnimationController.eventDispatcher.removeEventListener(AnimationController.ANIMATION_COMPLETE, introAnimationComplete);
  125. }
  126. private function rollOverInstrumentButton(event:MouseEvent):void
  127. {
  128. var instrumentButton:InstrumentButton = InstrumentButton(event.target);
  129. AnimationController.changeCharacterAnimation(instrumentButton.instrument.preSelectAnimation);
  130. }
  131. private function clickedInstrumentButton(event:MouseEvent):void
  132. {
  133. var instrumentButton:InstrumentButton = InstrumentButton(event.target);
  134. AnimationController.changeCharacterAnimation(instrumentButton.instrument.postSelectAnimation);
  135. AnimationController.eventDispatcher.addEventListener(AnimationController.ANIMATION_COMPLETE, postSelectAnimationComplete);
  136. _graphic.play();
  137. Main.level.song = _song;
  138. Main.level.instrument = instrumentButton.instrument;
  139. }
  140. private function postSelectAnimationComplete(event:Event):void
  141. {
  142. AnimationController.eventDispatcher.removeEventListener(AnimationController.ANIMATION_COMPLETE, postSelectAnimationComplete);
  143. proceedToPlaying();
  144. }
  145. private function proceedToPlaying():void
  146. {
  147. new QuickFrameTimer(1, Main.level.showInstructions);
  148. }
  149. override public function removed():void
  150. {
  151. destroy();
  152. }
  153. override public function destroy():void
  154. {
  155. removeInstrumentButtons();
  156. _graphic.continueButton.removeEventListener(MouseEvent.CLICK, selectSong);
  157. }
  158. }
  159. }
  160. import flash.display.MovieClip;
  161. import flash.display.SimpleButton;
  162. import flash.events.*;
  163. import instruments.Instrument;
  164. import songs.Song;
  165. class InstrumentButton extends EventDispatcher
  166. {
  167. public var instrument:Instrument;
  168. public var button:SimpleButton;
  169. public function InstrumentButton(button:SimpleButton, instrument:Instrument)
  170. {
  171. this.instrument = instrument;
  172. this.button = button;
  173. button.addEventListener(MouseEvent.CLICK, clickButton);
  174. button.addEventListener(MouseEvent.MOUSE_OVER, rollOverButton);
  175. }
  176. private function rollOverButton(event:MouseEvent):void
  177. {
  178. dispatchEvent(event);
  179. }
  180. private function clickButton(event:MouseEvent):void
  181. {
  182. dispatchEvent(event);
  183. }
  184. public function destroy():void
  185. {
  186. button.removeEventListener(MouseEvent.CLICK, clickButton);
  187. button.removeEventListener(MouseEvent.MOUSE_OVER, rollOverButton);
  188. }
  189. }
  190. class SongButton extends EventDispatcher
  191. {
  192. public var song:Song;
  193. public var button:GSongButton;
  194. public function SongButton(button:GSongButton, song:Song)
  195. {
  196. this.song = song;
  197. this.button = button;
  198. button.textField.text = song.name;
  199. button.addEventListener(MouseEvent.CLICK, clickButton);
  200. }
  201. private function clickButton(event:MouseEvent):void
  202. {
  203. dispatchEvent(event);
  204. }
  205. public function destroy():void
  206. {
  207. button.removeEventListener(MouseEvent.CLICK, clickButton);
  208. }
  209. }