0

由于我是 Flash CS6 actionscript 3 的新手,有人可以具体地举一个详细的例子,向我解释一下如何向我的播放器添加 2 首歌曲,并且仍然使用当前的播放和暂停按钮来控制它们吗?

非常感谢!

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;

var mySound:Sound = new heyjude();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;

pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);

function onClickPause(e:MouseEvent):void{
lastPosition = myChannel.position;  
myChannel.stop();
}

play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void{
myChannel.stop()
myChannel = mySound.play(lastPosition);
}
4

1 回答 1

0

我只是假设有四个标题/标题按钮和四个标题通道来编写这个示例代码。我有一个标题声音的声道。我可以停止或暂停或播放声音。简单地说,我可以使用titleChannel.

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;

var titleSound1:Sound = new TitleSound1();
var titleSound2:Sound = new TitleSound2();
var titleSound3:Sound = new TitleSound3();
var titleSound4:Sound = new TitleSound4();

var mySound:Sound = new heyjude();

var titleChannel:SoundChannel;
var myChannel:SoundChannel;

var lastPosition:Number = 0;

titleBtn1.addEventListener(MouseEvent.CLICK, title1Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title2Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title3Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title4Selected);

play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);
pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);

function title1Selected(e:MouseEvent){
    if(titleChannel != null) titleChannel.stop();
    titleChannel = titleSound1.play();
}

function title2Selected(e:MouseEvent){
    if(titleChannel != null) titleChannel.stop();
    titleChannel = titleSound2.play();
}

function title3Selected(e:MouseEvent){
    if(titleChannel != null) titleChannel.stop();
    titleChannel = titleSound3.play();
}

function title4Selected(e:MouseEvent){
    if(titleChannel != null) titleChannel.stop();
    titleChannel = titleSound4.play();
}

function onClickPause(e:MouseEvent):void{
    lastPosition = myChannel.position;  
    myChannel.stop();
}

function onClickPlay(e:MouseEvent):void{
    myChannel.stop()
    myChannel = mySound.play(lastPosition);
}

myChannel如果您想像其他声音一样暂停和恢复标题声音的播放,请按照您在功能onClickPause和中所做的相同操作onClickPlay

注意:如果您想为某个事件播放声音(例如背景或爆炸期间或其他),并且该事件经常发生,那么请不要犹豫,为该声音创建一个SoundChannel实例来控制该声音。

于 2012-09-24T04:42:09.863 回答