home *** CD-ROM | disk | FTP | other *** search
/ Champak 48 / cdrom_image.iso / Games / breakawish.swf / scripts / __Packages / Library / Sound / SoundItem.as next >
Encoding:
Text File  |  2007-09-28  |  5.7 KB  |  224 lines

  1. class Library.Sound.SoundItem
  2. {
  3.    var sndObj;
  4.    var sLinkage;
  5.    var mcRef;
  6.    var nRemainingLoop;
  7.    var nCurrentVolume;
  8.    var nTargetVolume;
  9.    var nFadeRate;
  10.    var sCategory;
  11.    var bFadeAtEnd;
  12.    var bMuted;
  13.    var bPaused;
  14.    var bNeedFreshStart;
  15.    var nCurrentTime;
  16.    var aEventListeners;
  17.    var bStopAfterFade;
  18.    static var FADE_RATE = 8;
  19.    static var FADE_AT_END_TIME = 800;
  20.    function SoundItem(__sndObject, __sLinkage, __nVolume, __nLoop, __sCategory, __mc)
  21.    {
  22.       this.sndObj = __sndObject;
  23.       this.sLinkage = __sLinkage;
  24.       this.mcRef = __mc;
  25.       this.nRemainingLoop = __nLoop;
  26.       this.nCurrentVolume = __nVolume;
  27.       this.nTargetVolume = __nVolume;
  28.       this.nFadeRate = Library.Sound.SoundItem.FADE_RATE;
  29.       this.sCategory = __sCategory;
  30.       this.bFadeAtEnd = false;
  31.       this.bMuted = false;
  32.       this.bPaused = false;
  33.       this.bNeedFreshStart = false;
  34.       this.sndObj.onSoundComplete = Library.Utils.Delegate.create(this,this.doSoundComplete);
  35.       this.doStart(__nLoop);
  36.       this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  37.       this.nCurrentTime = 0;
  38.       this.aEventListeners = new Array();
  39.    }
  40.    function doEnterFrame()
  41.    {
  42.       if(!this.bPaused)
  43.       {
  44.          if(this.nCurrentTime > this.sndObj.position)
  45.          {
  46.             this.nRemainingLoop = this.nRemainingLoop - 1;
  47.          }
  48.          this.nCurrentTime = this.sndObj.position;
  49.       }
  50.       this.doCheckFadeAtEnd();
  51.       this.doManageFade();
  52.    }
  53.    function doAddListener(__oListener)
  54.    {
  55.       this.aEventListeners.push(__oListener);
  56.    }
  57.    function doRemoveListener(__oListener)
  58.    {
  59.       var _loc2_ = 0;
  60.       while(_loc2_ < this.aEventListeners.length)
  61.       {
  62.          if(this.aEventListeners[_loc2_] == __oListener)
  63.          {
  64.             delete this.aEventListeners[_loc2_];
  65.             this.aEventListeners.splice(_loc2_,1);
  66.          }
  67.          _loc2_ = _loc2_ + 1;
  68.       }
  69.    }
  70.    function doSoundComplete()
  71.    {
  72.       if(this.bNeedFreshStart && this.nRemainingLoop > 1)
  73.       {
  74.          this.sndObj.start(0,this.nRemainingLoop - 1);
  75.       }
  76.       else
  77.       {
  78.          this.doManageEndEvent();
  79.       }
  80.    }
  81.    function doUpdateSound()
  82.    {
  83.       this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  84.    }
  85.    function doMute()
  86.    {
  87.       this.bMuted = true;
  88.       this.doUpdateSound();
  89.    }
  90.    function doUnMute()
  91.    {
  92.       this.bMuted = false;
  93.       this.doUpdateSound();
  94.    }
  95.    function doPause()
  96.    {
  97.       this.sndObj.stop();
  98.       this.bPaused = true;
  99.    }
  100.    function doResume()
  101.    {
  102.       this.bPaused = false;
  103.       this.bNeedFreshStart = true;
  104.       this.sndObj.start(this.nCurrentTime / 1000,1);
  105.    }
  106.    function doStart(__nLoop)
  107.    {
  108.       if(__nLoop == undefined)
  109.       {
  110.          __nLoop = 1;
  111.       }
  112.       if(this.sLinkage.indexOf(".mp3") != -1)
  113.       {
  114.          this.sndObj.start(0.015,__nLoop);
  115.       }
  116.       else
  117.       {
  118.          this.sndObj.start(0,__nLoop);
  119.       }
  120.    }
  121.    function doStop()
  122.    {
  123.       this.sndObj.stop();
  124.       this.doManageEndEvent();
  125.    }
  126.    function doFadeTo(__nVolume, __bStopAndDelete)
  127.    {
  128.       if(__bStopAndDelete == undefined)
  129.       {
  130.          __bStopAndDelete = true;
  131.       }
  132.       this.bStopAfterFade = __bStopAndDelete;
  133.       this.nTargetVolume = __nVolume;
  134.    }
  135.    function setFadeRate(__nRate)
  136.    {
  137.       if(__nRate == undefined)
  138.       {
  139.          __nRate = Library.Sound.SoundItem.FADE_RATE;
  140.       }
  141.       this.nFadeRate = __nRate;
  142.    }
  143.    function setFadeAtEnd(__bFadeAtEnd)
  144.    {
  145.       this.bFadeAtEnd = true;
  146.    }
  147.    function setPan(__nPan)
  148.    {
  149.       this.sndObj.setPan(__nPan);
  150.    }
  151.    function get Category()
  152.    {
  153.       return this.sCategory;
  154.    }
  155.    function get LinkageName()
  156.    {
  157.       return this.sLinkage;
  158.    }
  159.    function doDestroy()
  160.    {
  161.       this.sndObj.stop();
  162.       delete this.sndObj;
  163.       this.mcRef.removeMovieClip();
  164.    }
  165.    function doCheckFadeAtEnd()
  166.    {
  167.       if(this.bFadeAtEnd)
  168.       {
  169.          if(this.nRemainingLoop == 1)
  170.          {
  171.             if(this.sndObj.duration - this.nCurrentTime <= Library.Sound.SoundItem.FADE_AT_END_TIME)
  172.             {
  173.                this.doFadeTo(0);
  174.             }
  175.          }
  176.       }
  177.    }
  178.    function doManageEndEvent()
  179.    {
  180.       var _loc2_ = 0;
  181.       while(_loc2_ < this.aEventListeners.length)
  182.       {
  183.          this.aEventListeners[_loc2_].doSoundEvent(Library.Sound.SoundManager.EVENT_SOUND_COMPLETE,this);
  184.          _loc2_ = _loc2_ + 1;
  185.       }
  186.       this.aEventListeners = new Array();
  187.       this.mcRef.removeMovieClip();
  188.       delete this.mcRef;
  189.       delete this.aEventListeners;
  190.       delete this.sndObj.onSoundComplete;
  191.       delete this.sndObj;
  192.    }
  193.    function doManageFade()
  194.    {
  195.       if(this.nCurrentVolume != this.nTargetVolume)
  196.       {
  197.          this.nCurrentVolume = Library.Utils.MoreMath.getReachNum(this.nCurrentVolume,this.nTargetVolume,this.nFadeRate);
  198.          this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  199.       }
  200.       if(this.nCurrentVolume <= 0 && this.bStopAfterFade)
  201.       {
  202.          this.sndObj.stop();
  203.          this.doManageEndEvent();
  204.       }
  205.    }
  206.    function returnComputedVolume(__nVolume)
  207.    {
  208.       var _loc2_ = undefined;
  209.       if(!this.bMuted && !Library.Sound.SoundManager.isCategoryMuted(this.sCategory))
  210.       {
  211.          var _loc3_ = Library.Sound.SoundManager.MasterVolume / 100;
  212.          var _loc4_ = Library.Sound.SoundManager.getCategoryVolume(this.sCategory) / 100;
  213.          _loc2_ = __nVolume;
  214.          _loc2_ *= _loc4_;
  215.          _loc2_ *= _loc3_;
  216.       }
  217.       else
  218.       {
  219.          _loc2_ = 0;
  220.       }
  221.       return _loc2_;
  222.    }
  223. }
  224.