BloggerAds廣告

相關文章

2011年2月24日 星期四

解決 AS2 Sound class 不能獨立調整聲音問題

暫時這blog 都未有人流, 小弟暫且假設到我blog 的人對AS都有認識

若各下真的看不懂的話歡迎私底下和小弟聯絡

不知道這個問題是否很多人都懂解決

沒所謂了,還是分享一下,也許對一些人會有幫助

請看看以下的code:
var withBGSound:Boolean = true;
var withEffectSound:Boolean = true;

var bgSound:Sound = new Sound();  //background music
var effectSound:Sound = new Sound();  //sound effect


init();

function init():Void {

  bgSound.attachSound("bg_sound");
  bgSound.start(0, 9999);  


  btnBGSoundSwitcher.onRelease = btnBGSoundSwitcher_onRelease;
  btnEffectSoundSwitcher.onRelease = btnEffectSoundSwitcher_onRelease;

}
//do somthing then play the effect sound
function shoot():Void {
  //do somthing


  if(withEffectSound){

   effectSound.attachSound("bomb_sound");
   effectSound.start(0, 1);
  }
}

//onRelease handler for btnBGSoundSwitcher, used to switch on/off the background music
function btnBGSoundSwitcher_onRelease():Void {
  if (withBGSound) {
   withBGSound = false;
   bgSound.setVolume(0);

  }else {
   withBGSound = true;
   bgSound.setVolume(100);
  }
}

//onRelease handler for btnEffectSoundSwitcher, used to switch on/off the effect sound
function btnEffectSoundSwitcher_onRelease():Void {
  if (withBGSound) {
   withBGSound = false;

   bgSound.setVolume(0);
  }else {
   withBGSound = true;
   bgSound.setVolume(100);
  }
}
如果我按下了btnBGSoundSwitcher將會發生甚麼事呢??

我猜大家都會覺得只餘下effectSound 會在響吧

不過對不起, 大家都錯了

結果是兩個聲都會一塊兒消失

AS2裡的Sound class 確實是很麻煩
根據文檔, Sound.setVolume()這個 method 是一個instance method

那就是說, 這個method 理論上是對Sound class 的instance生效

可是不知怎的這個 method 會對全世界的Sound instance 生效
解決方法就是用channel 的概念 --- 每個聲源都獨立的聲軌負責

Sound class的constructor帶一個參數 -- target:Object

不過另一個問題出現

他說是Object, 小弟今天測試過是不行的

一定要是MovieClip

嗚.....這樣要寫個SoundChannel class 就好煩了

既然要用MovieClip

那就是說我現在有兩個channel -- 一個播音樂, 一個播音效

就要用兩個MovieClip了

其實....AS2 裡我不太喜歡好段段開粒不是用來display 的MovieClip

改寫頭8行:
var withBGSound:Boolean = true;

var withEffectSound:Boolean = true;

var mcBGSoundChannel:MovieClip = this.createEmptyMovieClip("mcBGSoundChannel", 0);

var mcEffectSoundChannel:MovieClip = this.createEmptyMovieClip("mcEffectSoundChannel", 1);

var bgSound:Sound = new Sound(mcBGSoundChannel);  //background music

var effectSound:Sound = new Sound(mcEffectSoundChannel);  //sound effect

再試試~

只餘下聲效了:D


沒有留言 :

張貼留言