Time for action – updating and drawing particles

  1. Add an Update() method to the Particle class:
    public override void Update(GameTime gameTime)
    {
        if (IsActive)
        {
            velocity += acceleration;
            if (velocity.Length() > maxSpeed)
            {
                velocity.Normalize();
                velocity *= maxSpeed;
            }
            TintColor = Color.Lerp(
                initialColor,
                finalColor,
                DurationProgress);
            remainingDuration--;
            base.Update(gameTime);
        }
    }
  2. Add a Draw() method to the Particle class:
    public override void Draw(SpriteBatch spriteBatch)
    {
        if (IsActive)
        {
            base.Draw(spriteBatch);
        }
    }

What just happened?

Both the Update() and the Draw() methods override the Sprite class' methods of the same name because we need to alter the behaviour associated with them. Both methods will only execute any code if the IsActive

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.