1

我在舞台上有 4 个按钮,我需要一个代码,使我能够单击其中一个,“无限”循环特定声音,当再次单击时,声音停止。声音最初没有播放。此外,当其中一个声音循环播放时,如果我按下另一个按钮,我希望之前的声音停止并播放新的声音。

为了帮助更多地可视化这一点,我将解释我的项目。我必须创建一个类似于在线吉他调音器的“应用程序”。这个功能是我想重新创建的:

http://www.gieson.com/Library/projects/utilities/tuner/

我什至不知道从哪里开始编码......非常感谢任何帮助。谢谢!

4

1 回答 1

0

实际代码很大程度上取决于您如何加载声音,因此我将为代码编写“骨架”。

var currentSound:Sound = null;
var currentSoundChannel:SoundChannel;

var sound1:Sound = /* Load me */
var sound2:Sound = /* Load me */


button1.addEventListener(MouseEvent.CLICK, playSound1);
function playSound1(event:MouseEvent)
{
    playSound(sound1);
}

button2.addEventListener(MouseEvent.CLICK, playSound2);
function playSound2(event:MouseEvent)
{
    playSound(sound2);
}


function playSound(sound:Sound):void
{
    if (currentSound != null)
    {
        // Stop the current music
        currentSoundChannel.stop();
    }

    if (currentSound == sound)
    {
        // Stop playing ANY sound
        currentSound = null;
        currentSoundChannel = null;
    }
    else
    {
        // Play a different sound
        currentSound = sound;
        currentSoundChannel = sound.play();
    }
}
于 2012-12-30T11:43:39.327 回答