Time for action – creating explosions

  1. Add the RandomDirection() helper method to the ExplosionManager class:
    public Vector2 randomDirection(float scale)
    {
        Vector2 direction;
        do
        {
            direction = new Vector2(
            rand.Next(0, 101) - 50,
            rand.Next(0, 101) - 50);
        } while (direction.Length() == 0);
        direction.Normalize();
        direction *= scale;
    
        return direction;
    }
  2. Add the AddExplosion() method to the ExplosionManager class:
    public void AddExplosion(Vector2 location, Vector2 momentum) { Vector2 pieceLocation = location - new Vector2(pieceRectangles[0].Width/2, pieceRectangles[0].Height/2); int pieces = rand.Next(minPieceCount, maxPieceCount + 1); for (int x = 0; x < pieces; x++) { ExplosionParticles.Add(new Particle( pieceLocation, texture, pieceRectangles[rand.Next(0,pieceRectangles.Count)], ...

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.