Question

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 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

Was it helpful?

Solution

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.

OTHER TIPS

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....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top