# Particle System in XNA - cannot draw particle

I'm trying to implement a simple particle system in my XNA project. I'm going by RB Whitaker's tutorial, and it seems simple enough. I'm trying to draw particles within my menu screen. Below I've included the code which I think is applicable.

I'm coming up with one error in my build, and it is stating that I need to create a new instance of the EmitterLocation from the particleEngine. When I hover over particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); it states that particleEngine is returning a null value. What could be causing this?

/// <summary>
/// Base class for screens that contain a menu of options. The user can
/// move up and down to select an entry, or cancel to back out of the screen.
/// </summary>

ParticleEngine particleEngine;

{
if (content == null)
{
content = new ContentManager(ScreenManager.Game.Services, "Content");
}
List<Texture2D> textures = new List<Texture2D>();
particleEngine = new ParticleEngine(textures, new Vector2(400, 240));

}

public override void Update(GameTime gameTime, bool otherScreenHasFocus,
bool coveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

// Update each nested MenuEntry object.
for (int i = 0; i < menuEntries.Count; i++)
{
bool isSelected = IsActive && (i == selectedEntry);

}

particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X,      Mouse.GetState().Y);

particleEngine.Update();
}

public override void Draw(GameTime gameTime)
{
// make sure our entries are in the right place before we draw them

GraphicsDevice graphics = ScreenManager.GraphicsDevice;
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
SpriteFont font = ScreenManager.Font;

spriteBatch.Begin();

// Draw stuff logic

spriteBatch.End();
particleEngine.Draw(spriteBatch);

}

• First, make sure that LoadContent is called, by printing a debug message or adding a breakpoint in the method. – William Mariager Jun 15 '12 at 0:12

you call to base.LoadContent() inside LoadContent and the method is not overrided.

Maybe adding the override tag and removing the content manager parameter will do the trick ...

public override void LoadContent()
{
}


I figured it out. Thanks guys, I learned a bit from your answers nonetheless.

I was trying to draw the particles in the wrong class. Instead of MenuScreen it should have been done in BackgroundScreen, which is where all of my Drawing is done for those screens.

• I know you might not be able to yet, but when you are able, mark this as the correct answer. Thanks – John McDonald Jun 15 '12 at 18:42

I think we need to see more code. Is GameScreen from the state management sample? What should be calling LoadContent? Is it a DrawableGameComponent? Don't forget the override keyword in the LoadContent declaration, otherwise you're just hiding the member function.

• Yes, it is the state management sample. I've also included a link to my Particle and Particle Engine classes to clarify: [link] pastebin.com/uD838rQd – Dave Voyles Jun 15 '12 at 11:40

I'm not that knowledgeable about particles but don't you need to go ParticleEngine particleEngine = new ParticleEngine(); ? Try that, no guarantee that's the fix though.

• oh wait no never mind I see it in the loadcontent. My bad :/ – user17125 Jun 15 '12 at 0:13

The only way that would be the case would be if LoadContent was never invoked. Looks like you probably want to double check the screen manager that you're using to make sure that it's going to call LoadContent before ever calling Update

• How can I make sure that Load Content is being called before update? – Dave Voyles Jun 15 '12 at 11:44