2
\$\begingroup\$

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>
abstract class MenuScreen : GameScreen

    ParticleEngine particleEngine;


    public void LoadContent(ContentManager content)
    {
        if (content == null)
        {
            content = new ContentManager(ScreenManager.Game.Services, "Content");
        }
        base.LoadContent();
        List<Texture2D> textures = new List<Texture2D>();
        textures.Add(content.Load<Texture2D>(@"gfx/circle"));
        textures.Add(content.Load<Texture2D>(@"gfx/star"));
        textures.Add(content.Load<Texture2D>(@"gfx/diamond"));
        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);

            menuEntries[i].Update(this, isSelected, gameTime);
        }

        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
        UpdateMenuEntryLocations();

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

        spriteBatch.Begin();

       // Draw stuff logic

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


    }
\$\endgroup\$
1
  • \$\begingroup\$ First, make sure that LoadContent is called, by printing a debug message or adding a breakpoint in the method. \$\endgroup\$
    – William
    Commented Jun 15, 2012 at 0:12

5 Answers 5

3
\$\begingroup\$

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()
{
}
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ I know you might not be able to yet, but when you are able, mark this as the correct answer. Thanks \$\endgroup\$ Commented Jun 15, 2012 at 18:42
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Jun 15, 2012 at 11:40
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ oh wait no never mind I see it in the loadcontent. My bad :/ \$\endgroup\$
    – user17125
    Commented Jun 15, 2012 at 0:13
0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ How can I make sure that Load Content is being called before update? \$\endgroup\$ Commented Jun 15, 2012 at 11:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .