/ProjetCar/src/apps/whackAMole/Mole.as

https://github.com/projetI10/projetI10
ActionScript | 77 lines | 59 code | 15 blank | 3 comment | 1 complexity | c69a607945f2a1fca3d721878e9e64d4 MD5 | raw file
  1. package apps.whackAMole {
  2. import com.transmote.utils.time.Timeout;
  3. import com.transmote.utils.ui.EmbeddedLibrary;
  4. import flash.display.MovieClip;
  5. import flash.display.Sprite;
  6. import flash.geom.Point;
  7. import flash.media.Sound;
  8. public class Mole extends Sprite {
  9. private static const MIN_ACTIVATE_TIME:Number = 2000;
  10. private static const ACTIVATE_TIME_RANGE:Number = 5000;
  11. private static const MIN_DUCK_TIME:Number = 350;
  12. private static const DUCK_TIME_RANGE:Number = 1250;
  13. private static const DOWN_ANIM_TIME:Number = 150;
  14. private static const HIT_GFX_TIME:Number = 1500;
  15. private var library:EmbeddedLibrary;
  16. private var gfx:MovieClip;
  17. private var hitSound:Sound;
  18. private var hitGfx:MovieClip;
  19. private var bActive:Boolean;
  20. private var animTimeout:Timeout;
  21. private var hitTimeout:Timeout;
  22. public function Mole (library:EmbeddedLibrary) {
  23. this.library = library;
  24. this.init();
  25. }
  26. public function onHit () :void {
  27. if (!this.bActive) { return; }
  28. this.hitSound.play();
  29. this.hitGfx.visible = true;
  30. this.hitTimeout = new Timeout(this.onHitTimeout, HIT_GFX_TIME);
  31. }
  32. private function init () :void {
  33. this.gfx = this.library.getSymbolInstance("mound") as MovieClip;
  34. this.gfx.scaleX = this.gfx.scaleY = 0.5;
  35. this.addChild(this.gfx);
  36. this.hitSound = this.library.getSymbolInstance("yell.mp3") as Sound;
  37. this.hitGfx = this.gfx.getChildByName("pow") as MovieClip;
  38. this.hitGfx.visible = false;
  39. this.onDownAnimComplete();
  40. }
  41. private function onDownAnimComplete () :void {
  42. // "down" animation complete; queue "up" animation
  43. this.bActive = false;
  44. this.gfx.gotoAndStop(1);
  45. this.animTimeout = new Timeout(this.onActivateTimeout, MIN_ACTIVATE_TIME + Math.random()*ACTIVATE_TIME_RANGE);
  46. }
  47. private function onActivateTimeout () :void {
  48. // begin "up" animation, queue "down" animation
  49. this.bActive = true;
  50. this.gfx.gotoAndPlay("up");
  51. this.animTimeout = new Timeout(this.onDuckTimeout, MIN_DUCK_TIME + Math.random()*DUCK_TIME_RANGE);
  52. }
  53. private function onDuckTimeout () :void {
  54. // begin "down" animation
  55. this.gfx.gotoAndPlay("down");
  56. this.animTimeout = new Timeout(this.onDownAnimComplete, DOWN_ANIM_TIME);
  57. }
  58. private function onHitTimeout () :void {
  59. this.hitGfx.visible = false;
  60. }
  61. }
  62. }