Particle System in XNA - cannot draw particle

Posted by Dave Voyles on Game Development See other posts from Game Development or by Dave Voyles
Published on 2012-06-15T00:03:01Z Indexed on 2012/06/15 15:28 UTC
Read the original article Hit count: 262

Filed under:
|

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


    }

© Game Development or respective owner

Related posts about XNA

Related posts about c#