好吧,我有一个非常奇怪的问题。我现在正在用 C#/MonoGame(在 Linux 上)编写一个简单的游戏。我正在尝试玩SoundEffect
. 当我打电话时Play()
(即使它已正确加载到LoadContent()
方法中)。NullReferenceException
它与消息一起抛出一个Object Reference not set to an instance of an object
。
这是代码的结构
public class MyGame : Game
{
// ..
private SoundEffect _sfx;
public PingGame ()
{
// ...
}
protected override void Initialize ()
{
// ...
}
protected override void LoadContent ()
{
// ...
// No errors here on loading it
_sfx = Content.Load<SoundEffect>("noise.wav");
}
protected override void Update (GameTime gameTime)
{
// ...
if (playSound)
{
// This is where the error is thrown
_sfx.Play();
}
// ...
}
protected override void Draw (GameTime gameTime)
{
// ..
}
}