home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / zombietypocalypse.swf / scripts / classes / graphical / controls / buttons / GenericButton.as next >
Encoding:
Text File  |  2008-09-15  |  5.8 KB  |  188 lines

  1. package classes.graphical.controls.buttons
  2. {
  3.    import classes.dispatchers.ButtonsEvents;
  4.    import classes.dispatchers.GameDispatcher;
  5.    import classes.dispatchers.MouseEventDispatcher;
  6.    import classes.dispatchers.MyMouseEvent;
  7.    import flash.display.MovieClip;
  8.    import flash.events.Event;
  9.    import flash.events.MouseEvent;
  10.    import flash.media.SoundChannel;
  11.    import game.SoundManager;
  12.    import game.ThisGameManager;
  13.    import main.GameManager;
  14.    
  15.    public class GenericButton extends MovieClip
  16.    {
  17.        
  18.       
  19.       private var locked:Boolean;
  20.       
  21.       private var clicked:Boolean;
  22.       
  23.       public var hit_areaaaaa:MovieClip;
  24.       
  25.       internal var hit:MovieClip;
  26.       
  27.       internal var mouseChannel:SoundChannel;
  28.       
  29.       internal var sManager:SoundManager;
  30.       
  31.       private var over:Boolean;
  32.       
  33.       public function GenericButton()
  34.       {
  35.          mouseChannel = new SoundChannel();
  36.          super();
  37.          sManager = ThisGameManager.getInstance().soundManager;
  38.          this.buttonMode = true;
  39.          clicked = false;
  40.          over = false;
  41.          locked = false;
  42.          mouseChildren = false;
  43.          addEventListener(MouseEvent.CLICK,onClick);
  44.          addEventListener(MouseEvent.ROLL_OVER,onRollingOver);
  45.          addEventListener(MouseEvent.ROLL_OUT,onRollingOut);
  46.          addEventListener(MouseEvent.MOUSE_DOWN,onMouseIsDown);
  47.          addEventListener(MouseEvent.MOUSE_UP,onMouseIsUp);
  48.          addEventListener(Event.REMOVED_FROM_STAGE,cleanUp);
  49.          GameDispatcher.buttonsDispatcher.addEventListener(ButtonsEvents.BUTTONS_UNLOCKED,refreshButton);
  50.          GameDispatcher.mouseDispatcher.addEventListener(MouseEventDispatcher.BUTTON_PRESSED,unclickButton);
  51.          goToLabel("off");
  52.       }
  53.       
  54.       protected function onRollingOut(param1:MouseEvent) : void
  55.       {
  56.          over = false;
  57.          if(GameManager.getInstance().AreButtonsEnabled && !locked)
  58.          {
  59.             if(currentLabel != "out")
  60.             {
  61.                goToLabel("out");
  62.                locked = true;
  63.             }
  64.          }
  65.       }
  66.       
  67.       protected function goToLabel(param1:String) : void
  68.       {
  69.          var doesLabelExist:Boolean = false;
  70.          var i:Number = NaN;
  71.          var e:Error = null;
  72.          var destinationLabel:String = param1;
  73.          doesLabelExist = false;
  74.          try
  75.          {
  76.             i = 0;
  77.             while(i < currentLabels.length)
  78.             {
  79.                if(currentLabels[i].name == destinationLabel)
  80.                {
  81.                   doesLabelExist = true;
  82.                }
  83.                i++;
  84.             }
  85.             if(!doesLabelExist)
  86.             {
  87.                e = new Error("there is no label with the name >" + destinationLabel + "< in the button with instance name >" + this.name + "<");
  88.                throw e;
  89.             }
  90.             gotoAndPlay(destinationLabel);
  91.          }
  92.          catch(e:Error)
  93.          {
  94.          }
  95.       }
  96.       
  97.       protected function onRollingOver(param1:MouseEvent) : void
  98.       {
  99.          over = true;
  100.          if(GameManager.getInstance().AreButtonsEnabled)
  101.          {
  102.             if(clicked && param1.buttonDown)
  103.             {
  104.                goToLabel("click");
  105.             }
  106.             else if(!clicked && !locked)
  107.             {
  108.                clicked = false;
  109.                goToLabel("on");
  110.                locked = true;
  111.                mouseChannel.stop();
  112.                if(!ThisGameManager.getInstance().mute)
  113.                {
  114.                   mouseChannel = sManager.mOver.play();
  115.                }
  116.             }
  117.          }
  118.       }
  119.       
  120.       protected function onMouseIsDown(param1:MouseEvent) : void
  121.       {
  122.          if(GameManager.getInstance().AreButtonsEnabled)
  123.          {
  124.             clicked = true;
  125.             GameDispatcher.mouseDispatcher.buttonPressed(this);
  126.             goToLabel("click");
  127.          }
  128.       }
  129.       
  130.       protected function unlockButton() : void
  131.       {
  132.          locked = false;
  133.          refreshButton(new Event(""));
  134.       }
  135.       
  136.       protected function unclickButton(param1:MyMouseEvent) : *
  137.       {
  138.          if(param1.callerObject != this)
  139.          {
  140.             this.clicked = false;
  141.          }
  142.       }
  143.       
  144.       protected function onMouseIsUp(param1:MouseEvent) : void
  145.       {
  146.          if(GameManager.getInstance().AreButtonsEnabled && clicked)
  147.          {
  148.             clicked = false;
  149.             goToLabel("off");
  150.          }
  151.          GameDispatcher.mouseDispatcher.buttonPressed(this);
  152.       }
  153.       
  154.       protected function cleanUp(param1:Event) : *
  155.       {
  156.          removeEventListener(MouseEvent.CLICK,onClick);
  157.          removeEventListener(MouseEvent.ROLL_OVER,onRollingOver);
  158.          removeEventListener(MouseEvent.ROLL_OUT,onRollingOut);
  159.          removeEventListener(MouseEvent.MOUSE_DOWN,onMouseIsDown);
  160.          removeEventListener(MouseEvent.MOUSE_UP,onMouseIsUp);
  161.          removeEventListener(Event.REMOVED_FROM_STAGE,cleanUp);
  162.          GameDispatcher.buttonsDispatcher.removeEventListener(ButtonsEvents.BUTTONS_UNLOCKED,refreshButton);
  163.          GameDispatcher.mouseDispatcher.removeEventListener(MouseEventDispatcher.BUTTON_PRESSED,unclickButton);
  164.       }
  165.       
  166.       protected function onClick(param1:MouseEvent) : void
  167.       {
  168.          if(GameManager.getInstance().AreButtonsEnabled)
  169.          {
  170.          }
  171.       }
  172.       
  173.       protected function refreshButton(param1:Event) : void
  174.       {
  175.          if(over && this.currentLabel != "on" && !clicked)
  176.          {
  177.             goToLabel("on");
  178.             locked = true;
  179.          }
  180.          if(!over && this.currentLabel != "off" && !clicked)
  181.          {
  182.             goToLabel("out");
  183.             locked = true;
  184.          }
  185.       }
  186.    }
  187. }
  188.