2013-12-23 30 views
0

我在XNA中发音,我想将它们添加到字典中,以便我可以随时存储和调用它们。例如,存储跳跃声音,在玩家类别中跳转时调用它。没有添加到字典容器中的声音

下面是代码:

this.soundEffectInfo = new SoundEffectInfo(this, "BackgroundSound", "Assets\\Sounds\\RaceSound", 0.9f, 20, 20, true); 

soundManager.add(soundEffectInfo); 
soundEffectInfo.SOUNDEFFECT.Play(); 

this.soundEffectInfo = new SoundEffectInfo(this, "JumpSound", "Assets\\Sounds\\JumpSound", 0.9f, 20, 20, true); 
soundManager.add(soundEffectInfo); 

soundEffectInfoSoundEffectInfo对象这需要在SoundEffectInstanceSoundManager是这些实例的类容器。

我得到的错误是:

型“System.NullReferenceException”未处理的异常发生在RATEV2.exe
其他信息:对象引用不设置到对象的实例。

+1

你究竟在哪里发生错误?我没有看到代码叫你跳跃声音... – Vogel612

回答

0

像你这样的气味没有实例化你的字典。

0

我需要看到整个班级去了解什么是错误的,而不是将所有声音效果放到一个字典中尝试创建一个静态类来保存对所有声音效果文件的引用,以下是我的操作方法在我的游戏:

public static class SoundEffects 
    { 
      public static SoundEffect jump; 

      public static void LoadSounds(ContentManager content) 
      { 
       jump = content.Load<SoundEffect>("location); 
      } 
    } 

然后,您可以只调用SoundEffects.jump.Play();所需要的任何地方,你也可以写一个静态SoundManager类中的类来调整容积,当需要的时候玩的东西。