C#/ XNA在不同的游戏状态下停止歌曲

时间:2013-04-13 01:41:04

标签: c# audio menu xna-4.0 states

我正在尝试在菜单状态下播放音乐,但在切换到我的游戏状态后切断。

1 个答案:

答案 0 :(得分:1)

MediaPlayer类有一个Play()和Stop()方法。您在这里是MSDN链接:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.mediaplayer.play.aspx http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.mediaplayer.stop.aspx

如果我没记错的话,你可以这样做:

// Variable declaration
Song menu_song;
Song game_song;
bool isInMenu; //I'll use this instead of the gamestate as example

//In the LoadContent method:
//remember to add the song to your project. In this case is in 
//a folder called "music"
 Content.Load<Song>("music/menuSongName"); 
 Content.Load<Song>("music/gameSongName"); 

//In Update method:

if (isInMenu) 
{
MediaPlayer.Stop(); //Stop the current audio...
MediaPlayer.Play(menu_song); //...and start playing the next.
}
else
{
MediaPlayer.Stop(); //Stop the current audio
MediaPlayer.Play(game_song); //...and start playing the next.
}

我没有检查过这段代码,如果你有问题请告诉我,我会检查一下。 =) 它与游戏状态相同,我想您将知道如何根据您的需求调整此代码。