Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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
C# 阻止音频文件重复XNA_C#_Audio_Timer_Xna - Fatal编程技术网

C# 阻止音频文件重复XNA

C# 阻止音频文件重复XNA,c#,audio,timer,xna,C#,Audio,Timer,Xna,我在游戏中有声音 private SoundEffect sirenSound; 我有一个模型,它使用碰撞检测来检测何时与敌方模型发生碰撞 if (enemySphere.Intersects(playerSphere)) { health--; sirenSound.Play(); } 这很好,只是我遇到的问题是,当两个模型发生碰撞时,每帧都会播放另一首歌 我想也许像这样的东西会有用 if(sirenSound.Play() == false) sirenSound.

我在游戏中有声音

private SoundEffect sirenSound;
我有一个模型,它使用碰撞检测来检测何时与敌方模型发生碰撞

if (enemySphere.Intersects(playerSphere))
{
    health--;
    sirenSound.Play();
}
这很好,只是我遇到的问题是,当两个模型发生碰撞时,每帧都会播放另一首歌

我想也许像这样的东西会有用

if(sirenSound.Play() == false)
   sirenSound.Play();
但是它不起作用,有人知道一种不需要使用计时器的快速解决方法吗?

SoundEffect上的
Play()
方法旨在用于火灾和遗忘的方式。对于您正在做的事情,您希望使用
SoundEffectInstance
,如文档所述

…就是这样的

private SoundEffectInstance sfxInstance;
private SoundEffect sfx;

// ...

if (sfxInstance == null)
    sfxInstance = sfx.CreateInstance();

if (sfxInstance != SoundState.Playing)
    sfxInstance.Play();