开发者

XNA: NullReferenceException when playing SoundEffect from another class

I'm both a C# and XNA noob, and I'm a bit stuck.

In my Game1 class, I have added a SoundEffect object. From within this class, I can then play the sound by using [objectname].Play();. E.g.

public开发者_如何学Python SoundEffect newSound;
newSound.Play();

However, I have another class which represents a sprite. If I try to ply the sound from within that class, I get a nullreference exception error. For example (within my sprite class):

Game1 newGame = new Game1();
newGame.newSound.Play();

I know this is a common error. I know thatit has something to do with initializing the object instance. My problem is that, while I have researched this extensively, and have found other solutions to this error, I don't understand why I'm receiving it.

That's why I haven't pasted my full code. What I'm wondering is, can anyone point me in the direction of a tutorial or article that can explain to me how this should work? I'd rather not just make this error disappear without fully understanding what the problem is.

Any help would be most appreciated. Thanks


The problem is, your sprite needs access to the instance of Game1 that is running the game loop, and that has initialised the SoundEffect. new Game1() is giving you a different instance, which isn't in the right state to do anything.

What would normally be done here is to have a constructor argument or a settable property on your Sprite class. I assume your Game1 class create your sprite at some point:

Sprite s = new Sprite();

And instead, you want to be able to pass the instance of Game1 to it:

Sprite s = new Sprite(this);

And you'll need to modify your sprite class so that it a) takes this new argument in its constructor, and b) stores this value into a field, so that you can access it later.

I'd be able to flesh this out a bit more if I could see your whole Sprite class, but I can appreciate it may be a little large to post here.


This it's going to be like assuming a lot of things....

You have Game1 class, that it's the main class that is running in the Update/Draw infinite loop to keep the game...

Then you have another class that let's call it Enemy, and in in the Update method of Game1 you make a call like Enemy.PlaySound()

In Enemy::PlaySound you want to play the sound that you initialized in the LoadContent of Game1 something like

public void PlaySound()
{
 Game1 newGame; //like assuming that with this you are pointing to the instance of Game1 that it's running and since it's not the instance of that class and it's not even initialized there is the NullReferenceException.....(I think)
newGame.NewSound.Play(); //Assuming againt that we have a property to access the NewSound
}

A lot of long shots here.....but it's kinda of unclear the question....

EDIT - After First Comment

That won't work either

It will work like this

public class Enemy
{
 ....

    public void PlaySound(Game1 newGame)
    {
        newGame.NewSound.Play();
    }
 ....
}

But passing the Game1 as a parameter to the Enemy Methods is not a good practice...IMO

There are a lot of good books, tutorials and frameworks that can guide you....

  • http://www.flatredball.com/
  • http://nuclexframework.codeplex.com/
  • http://create.msdn.com/en-US/education/gamedevelopment
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜