home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Puzzle / Clusterz / Clusterz.swf / scripts / ENGINE / CORE / OSound.as < prev    next >
Encoding:
Text File  |  2008-09-12  |  6.3 KB  |  212 lines

  1. package ENGINE.CORE
  2. {
  3.    import flash.events.Event;
  4.    import flash.events.IOErrorEvent;
  5.    import flash.media.Sound;
  6.    import flash.media.SoundChannel;
  7.    import flash.media.SoundTransform;
  8.    import flash.net.URLRequest;
  9.    import flash.utils.Dictionary;
  10.    
  11.    public class OSound
  12.    {
  13.       
  14.       private static var iMusicInd:int = 0;
  15.       
  16.       private static var iSoundsObjects:Array;
  17.       
  18.       private static var iSounds:Dictionary = new Dictionary(true);
  19.       
  20.       private static var iMusicPlayList:Array = new Array();
  21.       
  22.       private static var iSoundVolume:Number = 1;
  23.       
  24.       private static var iMusicChannel:SoundChannel;
  25.       
  26.       private static var iMusicVolume:Number = 1;
  27.       
  28.       private static var iMusic:Sound;
  29.        
  30.       
  31.       public function OSound()
  32.       {
  33.          super();
  34.       }
  35.       
  36.       public static function PlaySoundInd(param1:int, param2:Number = 0) : Boolean
  37.       {
  38.          if(OSound.iSoundVolume == 0)
  39.          {
  40.             return false;
  41.          }
  42.          param1 = Math.max(param1,0);
  43.          param1 = Math.min(param1,OSound.iSoundsObjects.length - 1);
  44.          return OSound.PlaySound(OSound.iSoundsObjects[param1],param2);
  45.       }
  46.       
  47.       public static function set MusicVolume(param1:Number) : void
  48.       {
  49.          var _loc2_:SoundTransform = null;
  50.          OSound.iMusicVolume = param1;
  51.          if(OSound.iMusicChannel)
  52.          {
  53.             if(param1 == 0)
  54.             {
  55.                OSound.StopMusic();
  56.             }
  57.             else
  58.             {
  59.                _loc2_ = OSound.iMusicChannel.soundTransform;
  60.                _loc2_.volume = param1;
  61.                OSound.iMusicChannel.soundTransform = _loc2_;
  62.             }
  63.          }
  64.          else if(OSound.iMusicVolume > 0)
  65.          {
  66.             OSound.PlayMusic();
  67.          }
  68.       }
  69.       
  70.       private static function OnMusicLoadComplete(param1:Event) : void
  71.       {
  72.          if(OSound.iMusicVolume == 0)
  73.          {
  74.             return;
  75.          }
  76.          OSound.iMusicChannel = iMusic.play(0,0,new SoundTransform(OSound.iMusicVolume,0));
  77.          OSound.RemoveMusic();
  78.          OSound.iMusicChannel.addEventListener(Event.SOUND_COMPLETE,OnMusicComplete);
  79.       }
  80.       
  81.       public static function set SoundVolume(param1:Number) : void
  82.       {
  83.          OSound.iSoundVolume = param1;
  84.       }
  85.       
  86.       private static function OnMusicLoadIOError(param1:Event) : void
  87.       {
  88.          OSound.RemoveMusic();
  89.          OSound.iMusicPlayList.splice(OSound.iMusicInd,1);
  90.          OSound.iMusicInd = OSound.iMusicInd > OSound.iMusicPlayList.length - 1 ? int(OSound.iMusicPlayList.length - 1) : OSound.iMusicInd;
  91.          OSound.PlayMusic();
  92.       }
  93.       
  94.       private static function OnMusicComplete(param1:Event) : void
  95.       {
  96.          PlayMusic();
  97.       }
  98.       
  99.       public static function PlaySound(param1:Class, param2:Number = 0) : Boolean
  100.       {
  101.          var _loc3_:Sound = null;
  102.          if(OSound.iSoundVolume == 0)
  103.          {
  104.             return false;
  105.          }
  106.          _loc3_ = OSound.iSounds[param1];
  107.          if(_loc3_ != null)
  108.          {
  109.             _loc3_.play(0,0,new SoundTransform(OSound.iSoundVolume,param2));
  110.          }
  111.          return _loc3_ != null;
  112.       }
  113.       
  114.       private static function RemoveMusic() : void
  115.       {
  116.          if(OSound.iMusic)
  117.          {
  118.             OSound.iMusic.removeEventListener(Event.COMPLETE,OSound.OnMusicLoadComplete);
  119.             OSound.iMusic.removeEventListener(IOErrorEvent.IO_ERROR,OSound.OnMusicLoadIOError);
  120.             OSound.iMusic = null;
  121.          }
  122.       }
  123.       
  124.       public static function StopMusic() : void
  125.       {
  126.          if(OSound.iMusicChannel)
  127.          {
  128.             OSound.iMusicChannel.removeEventListener(Event.SOUND_COMPLETE,OnMusicComplete);
  129.             OSound.iMusicChannel.stop();
  130.             OSound.iMusicChannel = null;
  131.          }
  132.       }
  133.       
  134.       public static function get SoundVolume() : Number
  135.       {
  136.          return OSound.iSoundVolume;
  137.       }
  138.       
  139.       public static function PlayListClear() : void
  140.       {
  141.          OSound.iMusicPlayList = new Array();
  142.       }
  143.       
  144.       public static function PlayListAdd(param1:String) : void
  145.       {
  146.          OSound.iMusicPlayList.push(param1);
  147.          OSound.iMusicInd = OSound.iMusicPlayList.length - 1;
  148.       }
  149.       
  150.       public static function PlayMusic() : void
  151.       {
  152.          var request:URLRequest = null;
  153.          if(OSound.iMusicVolume == 0 || !OSound.iMusicPlayList.length)
  154.          {
  155.             return;
  156.          }
  157.          OSound.StopMusic();
  158.          OSound.iMusicInd = (OSound.iMusicInd + 1) % OSound.iMusicPlayList.length;
  159.          request = new URLRequest(OSound.iMusicPlayList[OSound.iMusicInd]);
  160.          OSound.iMusic = new Sound();
  161.          OSound.iMusic.addEventListener(Event.COMPLETE,OSound.OnMusicLoadComplete);
  162.          OSound.iMusic.addEventListener(IOErrorEvent.IO_ERROR,OSound.OnMusicLoadIOError);
  163.          try
  164.          {
  165.             OSound.iMusic.load(request);
  166.          }
  167.          catch(e:Error)
  168.          {
  169.          }
  170.       }
  171.       
  172.       public static function RegisterEmbedSounds(param1:Array) : Boolean
  173.       {
  174.          var _loc2_:int = 0;
  175.          var _loc3_:Sound = null;
  176.          OSound.iSoundsObjects = param1;
  177.          _loc2_ = 0;
  178.          while(_loc2_ < param1.length)
  179.          {
  180.             _loc3_ = new param1[_loc2_]() as Sound;
  181.             OSound.iSounds[param1[_loc2_]] = _loc3_;
  182.             _loc2_++;
  183.          }
  184.          return true;
  185.       }
  186.       
  187.       public static function PlaySoundRandom(param1:Number = 0, param2:int = 0, param3:int = -1) : Boolean
  188.       {
  189.          var _loc4_:int = 0;
  190.          if(OSound.iSoundVolume == 0)
  191.          {
  192.             return false;
  193.          }
  194.          param2 = Math.max(param2,0);
  195.          param2 = Math.min(param2,OSound.iSoundsObjects.length - 1);
  196.          if(param3 < 0)
  197.          {
  198.             param3 = int(OSound.iSoundsObjects.length - 1);
  199.          }
  200.          param3 = Math.max(param3,0);
  201.          param3 = Math.min(param3,OSound.iSoundsObjects.length - 1);
  202.          _loc4_ = OUtils.Random(param2,param3);
  203.          return OSound.PlaySound(OSound.iSoundsObjects[_loc4_],param1);
  204.       }
  205.       
  206.       public static function get MusicVolume() : Number
  207.       {
  208.          return OSound.iMusicVolume;
  209.       }
  210.    }
  211. }
  212.