Time for action - creating the ScoreZoom class

  1. Add a new class file called ScoreZoom.cs to the Game1 class.
  2. Add the following using directive to the top of the file:
    using Microsoft.Xna.Framework.Graphics;
    
  3. Add the following declarations to the ScoreZoom class:
    public string Text;
    public Color DrawColor;
    private int displayCounter;
    private int maxDisplayCount = 30;
    private float scale = 0.4f;
    private float lastScaleAmount = 0.0f;
    private float scaleAmount = 0.4f;
    
  4. Add the Scale read-only property to the ScoreZoom class:
    public float Scale
    {
    get { return scaleAmount * displayCounter; }
    }
    
  5. Add a Boolean property to indicate when the ScoreZoom has finished displaying:
    public bool IsCompleted
    {
    get { return (displayCounter > maxDisplayCount); }
    }
    
  6. Create a constructor for the ScoreZoom class:
    public ScoreZoom(string displayText, Color fontColor)
    {
    Text = displayText;
    DrawColor = fontColor;
    displayCounter = 0;
    }
    
  7. Add an Update() method to the ScoreZoom class:
    public void Update()
    {
    scale += lastScaleAmount + scaleAmount;
    lastScaleAmount += scaleAmount;
    displayCounter++;
    }
    

What just happened?

The ScoreZoom class holds some basic information about a piece of text and how it will be displayed to the screen. The number of frames the text will be drawn for are determined by displayCounter and maxDisplayCount.

To manage the scale, three variables are used: scale contains the actual scale size that will be used when drawing the text, lastScaleAmount holds the amount the scale was increased by during the previous frame, and scaleAmount determines the growth in the scale factor during each frame.

You can see how this is used in the Update() method. The current scale is increased by both the lastScaleAmount and scaleAmount. lastScaleAmount is then increased by the scale amount. This results in the scale growing in an exponential fashion instead of increasing linearly by a scaleAmount for each frame. This will give the text a zooming effect as it starts growing slowly and then speeds up rapidly to fill the screen.

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

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