Export (0) Print
Expand All

How To: Scale a Sprite

This article demonstrates how to scale a sprite using a uniform scale, nonuniform scale, or a destination rectangle.

The Complete Sample

The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

To draw a scaled sprite with a uniform scale

  1. Follow steps 1–4 of How To: Draw a Sprite.
  2. In your Update method, determine how your sprite will be scaled. The normal size of the sprite is multiplied by the scale specified, so a value of 1.0 draws the sprite full size, where 0.5 will draw it half-sized and 2.0 will draw it at twice its original size.

    protected float scale = 1f;
    protected override void Update(GameTime gameTime)
    {
        ...
    
        // The time since Update was called last.
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
    
        // TODO: Add your game logic here.
        scale += elapsed;
        scale = scale % 6;
    
        base.Update(gameTime);
    }
    
  3. When drawing the sprite, specify the scale of the sprite as a parameter to draw. Specifying a floating-point scale parameter scales the sprite evenly in both the x and y directions.

    protected virtual void DrawForeground( SpriteBatch Batch )
    {
        Rectangle safeArea = GetTitleSafeArea( .8f );
        Vector2 position = new Vector2( safeArea.X, safeArea.Y );
        Batch.Begin();
        Batch.Draw( SpriteTexture, position, null,
            Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f );
        Batch.End();
    }
    
  4. When all the sprites have been drawn, call End on your SpriteBatch object.

To draw a scaled sprite using a nonuniform scale

  1. Follow steps 1–5 of How To: Draw a Sprite.
  2. In your Update method, determine how your sprite will be scaled along each axis, and store those values in a Vector2 object.

    protected Vector2 nonuniformscale = Vector2.One;
    protected override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
        float basescale = nonuniformscale.Y;
        basescale += (float)gameTime.ElapsedGameTime.TotalSeconds;
        basescale = basescale % 6;
        nonuniformscale.Y = basescale;
        nonuniformscale.X = basescale * .8f;
    }
    
  3. When drawing the sprite, specify the scale of the sprite using the Vector2 object that you updated earlier. Specifying a Vector2 scales the sprite independently in both the x and y directions.

    protected override void DrawForeground(SpriteBatch batch)
    {
        Rectangle safeArea = GetTitleSafeArea(.8f);
        Vector2 position = new Vector2(safeArea.X, safeArea.Y);
        batch.Begin();
        batch.Draw(SpriteTexture, position, null, Color.White, 0, Vector2.Zero,
            nonuniformscale, SpriteEffects.None, 0);
        batch.End();
    }
    
  4. When all of the sprites have been drawn, call End on your SpriteBatch object.

To draw a scaled sprite using a destination rectangle

  1. Follow steps 1–5 of How To: Draw a Sprite.
  2. In your Update method, construct a rectangle that defines where on screen the sprite will be drawn. This rectangle does not need to be the same shape or size as the original sprite; each dimension of the sprite is scaled independently to fit the destination rectangle.

    protected Rectangle destrect;
    protected override void Update(GameTime gameTime)
    {
        destrect = new Rectangle();
        Rectangle safeArea = GetTitleSafeArea(.8f);
        destrect.X = safeArea.X;
        destrect.Y = safeArea.Y;
        destrect.Width = (int)scale * 100;
        destrect.Height = (int)scale * 80;
        base.Update(gameTime);
    }
    
  3. When drawing the sprite, specify the destination rectangle as a parameter to Draw. The sprite will be drawn, filling the destination rectangle.

    protected override void DrawForeground(SpriteBatch Batch)
    {
        Batch.Begin();
        Batch.Draw(SpriteTexture, destrect, Color.White);
        Batch.End();
    }
    
  4. When all of the sprites have been drawn, call End on your SpriteBatch object.

Community Additions

ADD
Show:
© 2014 Microsoft