gpt4 book ai didi

actionscript-3 - 如何在 AS3 中控制声音/音乐播放黑屏

转载 作者:行者123 更新时间:2023-12-02 23:16:37 25 4
gpt4 key购买 nike

我正在用 AS3 制作游戏。我想在每个背景中加入不同的音乐。

到目前为止,我已经成功地在玩家进入场景时开始播放音乐。但是如果他出去了,音乐还在播放……(我希望音乐在玩家离开场景时停止)。

我不明白为什么音乐还在播放。这是我的代码:

public function newBackground(thisBack:String):void{
var room = back.currentBack;
switch (thisBack){
case "maisonExt":
var mySound:Sound = new exterieur ();
mySound.play ();

我的 mp3 文件名为“exterieur.mp3”。

如果您能想到任何事情,那将是一个很大的帮助。

谢谢 !

最佳答案

您需要再次停止音乐。如果您不停止它,它将继续播放:)

当你调用 .play 时,它会返回一个 SoundChannel,它可以让你控制播放——改变位置、停止播放,以及访问让你控制音量的 SoundTransform。以下是一些示例代码:

public var currentSoundChannel:SoundChannel;

public function newBackground(thisBack:String):void
{
var room = back.currentBack;
if (currentSoundChannel != null)
{
currentSoundChannel.stop()
}
var nextSong:Sound;
switch (thisBack){
case "maisonExt":
nextSong = new exterieur ();
break;
}
currentSoundChannel = nextSong.play();
}

如果你想了解更多关于声音的知识,我会推荐这个教程 http://gamedev.michaeljameswilliams.com/2009/03/03/avoider-game-tutorial-9/ .它是一个更大的教程的一部分,它教你游戏制作的许多方面,带你从新手到能够创建游戏。你真的应该检查一下。

关于actionscript-3 - 如何在 AS3 中控制声音/音乐播放黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17132189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com