2012-06-05 44 views
0

我是新的AS3,我正在建立一個完整的Flash網站,我試圖包括播放,暫停和停止按鈕(播放和暫停是同一個按鈕)的背景音樂。問題是音樂播放器在頁面加載時自動播放,當我點擊我網站的HOME按鈕時,音樂再次播放,並與首先播放的音樂重疊,那麼我應該怎麼做......提前感謝音樂重疊在Flash ActionScript 3

stop(); 

function goHome (e:MouseEvent):void{ 
gotoAndStop("HOME"); 
} 
home_btn.addEventListener(MouseEvent.CLICK, goHome); 

function goAbout (e:MouseEvent):void{ 
gotoAndStop("COMPANY"); 
} 
company_btn.addEventListener(MouseEvent.CLICK, goAbout); 

function goTestimony (e:MouseEvent):void{ 
gotoAndStop("TESTIMONY"); 
} 
news_btn.addEventListener(MouseEvent.CLICK, goTestimony); 

function goProduct (e:MouseEvent):void{ 
gotoAndStop("PRODUCT"); 
} 
product_btn.addEventListener(MouseEvent.CLICK, goProduct); 

function goContact (e:MouseEvent):void{ 
gotoAndStop("CONTACT"); 
} 
contacts_btn.addEventListener(MouseEvent.CLICK, goContact); 

//imports the necessary as events 
import flash.events.Event 
import flash.events.MouseEvent; 

var isPlaying:Boolean = new Boolean(); 
var pausePosition:Number = new Number(); 

//Create an instance of the Sound class 
var soundClip:Sound = new Sound(); 
//Create a new SoundChannel Object 
var sndChannel:SoundChannel = new SoundChannel(); 

//Load sound using URLRequest 
soundClip.load(new URLRequest("thousandyears.mp3")); 

//Create an event listener that wll update once sound has finished loading 
soundClip.addEventListener(Event.COMPLETE, onComplete, false, 0, true); 

controller.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true); 
stop_btn.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true); 

function onComplete(evt:Event):void { 
    //Play loaded sound 
    sndChannel = soundClip.play(); 
    isPlaying = true; 
} 

function btnPressController(evt:MouseEvent):void 
{ 
    switch(isPlaying) 
    { 
     case true: 
      controller.gotoAndStop(2); 
      pausePosition = sndChannel.position; 
      sndChannel.stop(); 
      isPlaying = false; 
     break; 
     case false: 
      controller.gotoAndStop(1); 
      sndChannel = soundClip.play(pausePosition); 
      isPlaying = true; 
     break; 
    } 
} 

function btnPressStop(evt:MouseEvent):void 
{ 
    pausePosition = 0; 
    sndChannel.stop(); 
    controller.gotoAndStop(2); 
    isPlaying = false; 
} 

回答

0

該代碼是否在第1幀? 第1幀標記爲'HOME'?

如果是這樣,我會在時間軸的開頭添加一個新幀(新幀1)並將代碼移入其中。這樣,代碼仍然會在第1幀,「HOME」幀將會是第2幀,依此類推。

你會需要稍微更新代碼:

gotoAndStop('HOME');

+0

它是在對動作框架2取代stop(),第一架是用於加載腳本。我試着將代碼移動到第一幀,它工作,音樂不重疊,但問題是我不能使用播放/停止按鈕,因爲播放/停止按鈕從第2幀開始。 – kaptenkeirin

+0

嗨,它的作品現在。我已經將腳本移動到第一幀,並將按鈕幀延伸到第一幀。感謝您的幫助 – kaptenkeirin

+0

如果它適合您,那麼請,您能接受答案嗎? – strah