Time for action – firing shots

  1. Add the FireShot() method to the ShotManager class:
    public void FireShot(
        Vector2 location,
        Vector2 velocity,
        bool playerFired)
    {
        Sprite thisShot = new Sprite(
            location, 
            Texture, 
            InitialFrame, 
            velocity);
    
        thisShot.Velocity *= shotSpeed;
    
        for (int x = 1; x < FrameCount; x++)
        {
            thisShot.AddFrame(new Rectangle(
                InitialFrame.X+(InitialFrame.Width * x),
                InitialFrame.Y,
                InitialFrame.Width,
                InitialFrame.Height));
        }
        thisShot.CollisionRadius = CollisionRadius;
        Shots.Add(thisShot);
    }

What just happened?

When a shot is fired, a new Sprite (called thisShot) is built using the parameters stored when the ShotManager class was constructed. The location and velocity are set according to the values passed to the FireShot() method. The ...

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.