簡體   English   中英

AS3代碼以暫停和播放Flash中的“整體”音頻(從一個場景到另一個場景)?

[英]AS3 Code to pause and play “overall” audio in flash (from scene to scene)?

我的問題是,如何在"scene 1"暫停音頻並使之播放,以便當您轉到"scene 3" (具有將要播放的附加音頻文件)時,其音頻通常仍處於靜音狀態但是,當您單擊“打開”按鈕時,它會播放場景中的相應歌曲,就像在游戲中一樣,您可以在主菜單上將音樂靜音,然后在整個游戲過程中將其取消靜音;

同樣,一般而言,我將如何制作播放/暫停按鈕(我假設“ if else ”語句可能有效,但不確定)

這實際上取決於您是要實際暫停音頻,停止音頻還是僅使音頻靜音。 每個都有不同的用途。

但是,就您的使用而言,聽起來好像您想使用“靜音”按鈕。 我敢肯定,有很多方法可以做到這一點。 我建議創建一個專門用於音頻的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時暫停它會給您帶來麻煩,因為音樂將從上次暫停的那一刻開始循環播放。

希望對您有所幫助! 在我的項目中,我已經使用這種代碼已有一年多的時間了,我從來沒有遇到過問題。

首先包括SoundMixer類:

導入flash.media.SoundMixer;

然后,要停止聲音,請調用stopAll方法:

SoundMixer.stopAll();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM