Time for action - declarations for the Sprite class

  1. Add a new class to the project called Sprite.cs.
  2. Update the class' using area to include:
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    
  3. Add the following declarations to the Sprite class:
    public Texture2D Texture;
    protected List<Rectangle> frames = new List<Rectangle>();
    private int frameWidth = 0;
    private int frameHeight = 0;
    private int currentFrame;
    private float frameTime = 0.1f;
    private float timeForCurrentFrame = 0.0f;
    private Color tintColor = Color.White;
    private float rotation = 0.0f;
    public int CollisionRadius = 0;
    public int BoundingXPadding = 0;
    public int BoundingYPadding = 0;
    protected Vector2 location = Vector2.Zero;
    protected Vector2 velocity = Vector2.Zero;
    

What just happened?

All of the animation frames for any individual sprite will be stored on the same sprite sheet, identified by the Texture variable. The frames list will hold a single Rectangle object for each animation frame defined for the sprite, while currentFrame stores the frame that is being displayed at any given time.

In order to control the animation, each frame is displayed for a pre-determined amount of time. This time, stored in frameTime, will be compared to timeForCurrentFrame to determine when currentFrame should be incremented. This is the same timing mechanism we used in Flood Control to pace input. The frameWidth and frameHeight variables will be assigned when the first frame of animation is established to provide shortcuts to these values.

The tintColor and rotation members store the implied information that will be used when the sprite is drawn to influence its appearance.

CollisionRadius, BoundingXPadding, and BoundingYPadding will all be used when we implement collision detection. We'll support both bounding circle and bounding box collisions. The CollisionRadius defines how large a circle to consider when determining if this sprite has collided with other sprites, while the padding values are used to "shrink" the frame size for collision detection, providing a cushion around the edges of the bounding box for collision purposes.

Finally, the location of the sprite on the screen is tracked via the location vector while the speed and direction at which the sprite is moving is stored in velocity. The velocity vector represents the distance (in pixels) that the sprite will travel in one second of game time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.240