1

我的问题是,如何暂停音频"scene 1"并让它播放,这样当你去"scene 3"(它附加了一个不同的音频文件将要播放)时,它的音频通常仍然是静音的,但是当你点击“on "按钮播放场景中的相应歌曲,有点像在游戏中您可以在主菜单中静音,然后在整个游戏过程中取消静音;

另外一般来说,我将如何制作播放/暂停按钮(我假设“ if else”语句可能有效但不确定)

4

2 回答 2

1

这实际上取决于您是要实际暂停音频、停止音频还是只是静音音频。每个都有不同的用途。

但是,对于您的使用,听起来您需要使用“静音”按钮。我敢肯定,有很多方法可以做到这一点。我推荐的是专门为音频创建一个 AS3 类。然后,您需要在文档类中设置此类。

转到文件 --> 新建...并选择 ActionScript 3.0 类。将其命名为“gamesounds.as”。在单击保存之前,请在与 .fla 相同的目录中创建一个新文件夹。将此文件夹命名为“gamecore”,然后将“gamesounds.as”保存在其中。我刚刚这样做的原因是,您需要将所有此类自定义类放在一起。

现在,这是您班级的基本结构:

package gamecore
{
    public class GameSounds
    {
        //constructor code
    }
}

在我们做任何其他事情之前,我们需要确保我们的游戏能够访问这个类。我们不想创建一百万个副本,因为这会破坏类的主要功能。打开您的文档类(创建一个新的文档类超出了此答案的范围。查找它。)当然,文档类必须与 gamecore 文件夹位于同一目录中(不在 gamecore 文件夹中)。

在文档类中的类声明上方,输入这行代码:

import gamecore.GameSounds;

然后,在你的类声明中,输入这一行:

public static var GameSounds:GameSounds = new GameSounds();

显然,保存,然后返回到您的 gamesounds.as 文件。这是您要使用的代码。我添加了注释来说明不同代码的作用。

我假设您已将所有歌曲导入到 .fla 的库中,并创建了它们的动作脚本绑定。您也可以修改它以在外部播放歌曲,但我不会在这里讨论。

以下应替换//constructor code

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;

//Create a variable to indicate whether the music is muted.
var isMuted:Boolean = false;

/*Create a string variable for the name of the song that should be playing. Alternatively, you could just name this an int and store the scene number. It all depends on what you want to do.*/
var songName:String;

//Create a sound channel for playing audio.
var musicChannel:SoundChannel = new SoundChannel();

//Import your songs.
var fooForYou:Sound = new fooForYou();
var iveBeenAFoo:Sound = new iveBeenAFoo();
var pityTheFoo:Sound = new pityTheFoo();

/*This function sets the target song based on location. Just pass it the integer of the stage. Alternatively, you can make this work with the stage name as a String.*/
function startMusic(targetScene:int):void
{
    /*Depending on the targetScene number, set the correct song name. Note these match the song declarations above.*/
    switch(targetScene)
    {
        case 1:
            songName = "fooForYou";
        break;
        case 2:
            songName = "iveBeenAFoo";
        break;
        case 3:
            songName = "pityTheFoo;
        break;
    }
    //Start the actual music playing.
    playMusic();
}

/*This function starts the music itself. Keep it separate, in case you need to bypass the startMusic code for some reason.*/
function playMusic():void
{
    //I'd imagine you want your music looped, so int.MAX_VALUE accommodates for that.
    musicChannel = this[songName].play(0, int.MAX_VALUE);

    //Mute or unmute depending on that variable above.
    adjustVolume();
}

//This function mutes or unmutes depending on the variable condition.
function adjustVolume():void
{
    //We create a SoundTransform.
    var transform:SoundTransform = musicChannel.soundTransform;

    //We set the volume to 0 or 1, depending on the isMuted variable.
    if(isMuted)
    {
        transform.volume = 0;
    }
    else
    {
        transform.volume = 1;
    }

    //We apply the transform to the song.
    musicChannel.soundTransform = transform;
}

/*This function is present for convenience's sake. Calling this adjusts the variable AND the music that is currently playing.*/
function setMute(mute:Boolean):void
{
    //Sets the isMuted variable to the mute argument.
    isMuted = mute;

    //Mutes or unmutes the currently playing sound.
    adjustVolume();
}

现在,您只需要在 .fla 本身中使用两个函数。我将假设“DocClass”是您的文档类的名称。在每个阶段开始时,调用这行代码,将参数中的“1”替换为阶段编号(或名称,如果您选择了该路线)。

DocClass.GameSounds.startMusic(1);

它将开始播放有效的音乐,但只有在音乐未设置为静音时才能听到。

将此代码添加到静音按钮以静音,将“true”替换为“false”以取消静音。

DocClass.GameSounds.setMute(true);

-- 关于暂停键,这个问题应该单独问。我会告诉你,如果你打算循环播放音乐,在使用 SoundChannel 时暂停它会给你带来一个问题,因为音乐将从上次暂停的点开始循环播放。

我希望这对你有帮助!我已经在我的项目中使用这种代码一年多了,而且我从来没有遇到过问题。

于 2012-06-19T23:03:33.913 回答
0

首先包括 SoundMixer 类:

导入 flash.media.SoundMixer;

然后停止声音调用 stopAll 方法:

SoundMixer.stopAll();

于 2012-06-19T20:58:33.257 回答