Skip to content

Commit 4ad65e6

Browse files
Added sample screenshots for each sample and gifs for moving images
1 parent a612b53 commit 4ad65e6

12 files changed

+48
-10
lines changed

articles/getting_to_know/howto/graphics/HowTo_Animate_Sprite.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ requireMSLicense: true
88

99
In this example, you will draw a sprite to the screen and then animate the sprite using a custom sprite animation class.
1010

11+
### End result
12+
13+
![The output of this tutorial](images/HowTo_AnimateSprite_Final.gif)
14+
15+
> [!NOTE]
16+
> Better animations can be done with better images and more frames, have a look at the [Platformer Sample](https://github.com/MonoGame/MonoGame.Samples/tree/3.8.1/Platformer2D) Provided by MonoGame for example.
17+
1118
## Requirements
1219

1320
The example assumes the texture you are loading contains multiple frames of the same size in a texture whose size is uniform (also known as a spritesheet), for example, the following spritesheet contains 8 Images of a character in different phases of motion, when player together it looks like it is animated.
1421

15-
![Character Spritesheet](../images/HowTo_SpriteAnimate_Spritesheet.PNG)
22+
![Character Spritesheet](images/HowTo_SpriteAnimate_Spritesheet.PNG)
1623

1724
Save the spritesheet to your content project and name it "**AnimatedCharacter**" (this name will used to reference it in the project).
1825

@@ -33,6 +40,9 @@ Save the spritesheet to your content project and name it "**AnimatedCharacter**"
3340
using Microsoft.Xna.Framework.Content;
3441
using Microsoft.Xna.Framework.Graphics;
3542

43+
/// <summary>
44+
/// A helper class for handling animated textures.
45+
/// </summary>
3646
public class AnimatedTexture
3747
{
3848
private int frameCount;
@@ -127,15 +137,17 @@ Save the spritesheet to your content project and name it "**AnimatedCharacter**"
127137
// The reference to the AnimatedTexture for the character
128138
private AnimatedTexture spriteTexture;
129139
// The rotation of the character on screen
130-
private const float Rotation = 0;
140+
private const float rotation = 0;
131141
// The scale of the character, how big it is drawn
132-
private const float Scale = 2.0f;
142+
private const float scale = 0.5f;
133143
// The draw order of the sprite
134-
private const float Depth = 0.5f;
144+
private const float depth = 0.5f;
135145

136146
public Game1()
137147
{
138-
spriteTexture = new AnimatedTexture(Vector2.Zero, Rotation, Scale, Depth);
148+
_graphics = new GraphicsDeviceManager(this);
149+
Content.RootDirectory = "Content";
150+
spriteTexture = new AnimatedTexture(Vector2.Zero, rotation, scale, depth);
139151
}
140152
```
141153

@@ -149,8 +161,8 @@ Save the spritesheet to your content project and name it "**AnimatedCharacter**"
149161
private Vector2 characterPos;
150162
// How many frames/images are included in the animation
151163
private const int frames = 8;
152-
// How many frames should be drawn each section, how fast does the animation run
153-
private const int framesPerSec = 3;
164+
// How many frames should be drawn each second, how fast does the animation run?
165+
private const int framesPerSec = 10;
154166

155167
protected override void LoadContent()
156168
{
@@ -180,7 +192,7 @@ Save the spritesheet to your content project and name it "**AnimatedCharacter**"
180192
}
181193
```
182194

183-
This is handled by AnimatedTexture's **UpdateFrame** method, which takes the elapsed seconds between updates as a parameter.
195+
This is handled by AnimatedTexture's **UpdateFrame** method, which takes the elapsed seconds between updates as a parameter, as shown below in the except from the "AnimatedTexture" class.
184196

185197
```csharp
186198
// class AnimatedTexture
@@ -208,6 +220,7 @@ Save the spritesheet to your content project and name it "**AnimatedCharacter**"
208220

209221
// TODO: Add your drawing code here
210222
_spriteBatch.Begin();
223+
// Replacing the normal SpriteBatch.Draw call to use the version from the "AnimatedTexture" class instead
211224
spriteTexture.DrawFrame(_spriteBatch, characterPos);
212225
_spriteBatch.End();
213226

articles/getting_to_know/howto/graphics/HowTo_Draw_A_Sprite.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ requireMSLicense: true
88

99
In any game, drawing basic textures to the screen is essential knowledge, whether it is for menus and background images, to 2D game textures and characters. In this sample we will walk though the steps needed to load a texture and then render it on the screen.
1010

11+
### End result
12+
13+
![The output of this tutorial](./images/HowTo_DrawSprite_Final.png)
14+
1115
## Requirements
1216

1317
The following texture will be used to render to the screen.
1418

15-
![Character Texture](../images/HowTo_DrawSprite_Character.png)
19+
![Character Texture](images/HowTo_DrawSprite_Character.png)
1620

1721
Save it to your content project and name it "**Character**" (this name will used to reference it in the project).
1822

@@ -26,7 +30,7 @@ Save it to your content project and name it "**Character**" (this name will used
2630
In this case, the example uses the **Content** member to load a texture from the MonoGame Framework Content Pipeline.
2731

2832
> [!IMPORTANT]
29-
> The texture must be in the project, with the same name passed to [ContentManager.Load](xref:Microsoft.Xna.Framework.Content.ContentManager#Microsoft_Xna_Framework_Content_ContentManager_Load__1_System_String_).
33+
> The texture must be in the project, with the same **Name** passed to [ContentManager.Load](xref:Microsoft.Xna.Framework.Content.ContentManager#Microsoft_Xna_Framework_Content_ContentManager_Load__1_System_String_). In this case the texture should be called "Character"!!
3034
3135
```csharp
3236
// The reference to the loaded sprite

articles/getting_to_know/howto/graphics/HowTo_Rotate_Sprite.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Rotating images and sprites is easy enough, but the main thing to keep in mind i
1010

1111
This guide walks you through calculating a new origin for images (the center in this case) and using that to determine to draw and rotate your sprite from.
1212

13+
### End result
14+
15+
![The output of this tutorial](./images/HowTo_RotateSprite_Final.gif)
16+
1317
## Drawing a Rotated Sprite
1418

1519
1. Follow the procedures of [Drawing a Sprite](HowTo_Draw_A_Sprite.md).
@@ -47,7 +51,9 @@ This guide walks you through calculating a new origin for images (the center in
4751
The angle is specified in radians, and it can be greater than two times `π`, but does not need to be.
4852

4953
```csharp
54+
// The angle at which to rotate and draw the sprite at
5055
private float rotationAngle;
56+
5157
protected override void Update(GameTime gameTime)
5258
{
5359
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
@@ -57,6 +63,7 @@ This guide walks you through calculating a new origin for images (the center in
5763
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
5864

5965
// TODO: Add your game logic here.
66+
// Simple roation logic to rotate the sprite in a clockwise direction over time
6067
rotationAngle += elapsed;
6168
float circle = MathHelper.Pi * 2;
6269
rotationAngle %= circle;
@@ -76,6 +83,7 @@ This guide walks you through calculating a new origin for images (the center in
7683

7784
// TODO: Add your drawing code here
7885
_spriteBatch.Begin();
86+
// Draw call updated to use a different overload with additional parameters, specifically the "Rotation angle" and "Origin".
7987
_spriteBatch.Draw(spriteTexture, spritePosition, null, Color.White, rotationAngle,
8088
spriteOrigin, 1.0f, SpriteEffects.None, 0f);
8189
_spriteBatch.End();

articles/getting_to_know/howto/graphics/HowTo_Scale_Sprite.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ There are several implementations to apply scaling to 2D sprites / textures, nam
1414

1515
The examples below detail each of the three methods.
1616

17+
### End result
18+
19+
![The output of this tutorial](./images/HowTo_ScaleSprite_Final.gif)
20+
1721
## Drawing a Scaled Sprite with a uniform scale
1822

1923
1. Follow the procedures of [Drawing a Sprite](HowTo_Draw_A_Sprite.md).
@@ -25,6 +29,7 @@ The examples below detail each of the three methods.
2529
```csharp
2630
private Vector2 spritePosition = Vector2.Zero;
2731
protected float spriteScale = 0f;
32+
2833
protected override void Update(GameTime gameTime)
2934
{
3035
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
@@ -50,6 +55,7 @@ The examples below detail each of the three methods.
5055

5156
// TODO: Add your drawing code here
5257
_spriteBatch.Begin();
58+
// Updated draw call using the overload that allows passing the "scale" of a sprite
5359
_spriteBatch.Draw(spriteTexture, spritePosition, null,
5460
Color.White, 0f, Vector2.Zero, spriteScale, SpriteEffects.None, 0f);
5561
_spriteBatch.End();

articles/getting_to_know/howto/graphics/HowTo_Tint_Sprite.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ requireMSLicense: true
88

99
Tinting sprites is an easy way to either animate a sprite (when it takes damage) or even to create different characters of different colors. It is quick and efficient to do and all you need is the color to tine with and a single change to your `SpriteBatch` draw call.
1010

11+
### End result
12+
13+
![The output of this tutorial](./images/HowTo_TintSprite_Final.png)
14+
1115
## Drawing a Tinted Sprite
1216

1317
1. Follow the procedures of [Drawing a Sprite](HowTo_Draw_A_Sprite.md).
@@ -16,7 +20,9 @@ Tinting sprites is an easy way to either animate a sprite (when it takes damage)
1620
In this example, the position of the mouse determines the Red, Green, values to apply to the sprite, the blue is fixed for simplicity.
1721

1822
```csharp
23+
// The color tint to apply to the sprite
1924
protected Color tint;
25+
2026
protected override void Update(GameTime gameTime)
2127
{
2228
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
@@ -39,6 +45,7 @@ Tinting sprites is an easy way to either animate a sprite (when it takes damage)
3945

4046
// TODO: Add your drawing code here
4147
_spriteBatch.Begin();
48+
// Note the final argument in the Draw call is changed from Color.White to the new "tint" property
4249
_spriteBatch.Draw(spriteTexture, spritePosition, tint);
4350
_spriteBatch.End();
4451

Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)