Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3暂停和播放代码;“总体”;flash中的音频(从一个场景到另一个场景)?_Actionscript 3_Flash - Fatal编程技术网

Actionscript 3 AS3暂停和播放代码;“总体”;flash中的音频(从一个场景到另一个场景)?

Actionscript 3 AS3暂停和播放代码;“总体”;flash中的音频(从一个场景到另一个场景)?,actionscript-3,flash,Actionscript 3,Flash,我的问题是,如何暂停“场景1”中的音频,并使其在转到“场景3”(其中附加了一个将要播放的不同音频文件)时,音频通常仍处于静音状态,但当您单击“on”按钮时,它会播放相应的场景歌曲,有点像在游戏中,你可以在主菜单中将音乐静音,然后在整个游戏过程中取消静音 另外,一般来说,我如何制作播放/暂停按钮(我假设一个“if-else”语句可能有效,但不确定)首先包括SoundMixer类: 导入flash.media.SoundMixer 然后,要停止声音,请调用stopAll方法: SoundMixer.

我的问题是,如何暂停
“场景1”
中的音频,并使其在转到
“场景3”
(其中附加了一个将要播放的不同音频文件)时,音频通常仍处于静音状态,但当您单击“on”按钮时,它会播放相应的场景歌曲,有点像在游戏中,你可以在主菜单中将音乐静音,然后在整个游戏过程中取消静音


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

首先包括SoundMixer类:

导入flash.media.SoundMixer

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


SoundMixer.stopAll()

首先包括SoundMixer类:

导入flash.media.SoundMixer

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


SoundMixer.stopAll()

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

但是,对于您的使用,听起来您需要使用“静音”按钮。我相信有很多方法可以做到这一点。我建议创建一个专门用于音频的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的库中,并创建了它们的actionscript绑定。您也可以修改它,以便在外部播放歌曲,不过我在这里不赘述

以下内容应替换
//构造函数代码

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”替换为阶段编号(或名称,如果您选择了该路线)

它将启动音乐,但只有在音乐未设置为静音时才能听到

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

DocClass.GameSounds.setMute(true);
-- 关于暂停按钮,应单独询问此问题。我会告诉你,如果你想循环你的音乐,在使用SoundChannel时暂停它会给你带来麻烦,因为音乐会从上次暂停的点开始循环


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

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

但是,对于您的使用,听起来您需要使用“静音”按钮。我相信有很多方法可以做到这一点。我建议创建一个专门用于音频的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的库中,并创建了它们的actionscript绑定。您也可以修改它,以便在外部播放歌曲,不过我在这里不赘述

以下内容应替换
//构造函数代码

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”替换为阶段编号(或名称,如果您选择了该路线)

它将启动音乐,但只有在音乐未设置为静音时才能听到

将此代码添加到静音按钮以静音,r