AS3代码暂停和播放"整体"闪存中的音频(从场景到场景)?

时间:2012-06-19 15:05:22

标签: actionscript-3 flash

我的问题是,如何暂停"scene 1"中的音频并将其设置为当您转到"scene 3"(其中附加的音频文件不同,即将播放)时,它仍然具有音频一般都很静音,但是当你点击“on”按钮时,它会播放相应的场景歌曲,有点像在游戏中你可以在主菜单上静音但是然后取消静音整个游戏过程;

另外一般情况下我如何制作播放/暂停按钮(我假设“if else”声明可能有效但不确定)

2 个答案:

答案 0 :(得分: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时暂停音乐会给你带来一个问题,因为音乐将从最后一次暂停时开始循环播放。

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

答案 1 :(得分:0)

首先包括SoundMixer类:

import flash.media.SoundMixer;

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

SoundMixer.stopAll();