Skip to content

Commit 9f1a486

Browse files
Merge pull request MonoGame#10 from AristurtleDev/08_the_animatedsprite_class
Add Chapter 08: The AnimatedSprite Class
2 parents 42e8169 + c989270 commit 9f1a486

File tree

7 files changed

+610
-1
lines changed

7 files changed

+610
-1
lines changed

articles/toc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126
href: tutorials/building_2d_games/06_optimizing_texture_rendering/
127127
- name: "07: The Sprite Class"
128128
href: tutorials/building_2d_games/07_the_sprite_class/
129+
- name: "07: The AnimatedSprite Class"
130+
href: tutorials/building_2d_games/08_the_animatedsprite_class/
129131
- name: Console Access
130132
href: console_access.md
131133
- name: Help and Support

articles/tutorials/building_2d_games/07_the_sprite_class/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public Sprite CreateSprite(string regionName)
190190
}
191191
```
192192

193-
## Using the `Sprite` Class
193+
## Using the Sprite Class
194194

195195
Let's adjust our game now to use the `Sprite` class instead of just the texture regions. Replace the contents of *Game1.cs* with the following:
196196

articles/tutorials/building_2d_games/08_the_animatedsprite_class/index.md

+534
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using Microsoft.Xna.Framework;
3+
using Microsoft.Xna.Framework.Graphics;
4+
using Microsoft.Xna.Framework.Input;
5+
using MonoGameLibrary.Graphics;
6+
7+
namespace MonoGameSnake;
8+
9+
public class Game1 : Game
10+
{
11+
private GraphicsDeviceManager _graphics;
12+
private SpriteBatch _spriteBatch;
13+
private AnimatedSprite _slime;
14+
private AnimatedSprite _bat;
15+
16+
public Game1()
17+
{
18+
_graphics = new GraphicsDeviceManager(this);
19+
Content.RootDirectory = "Content";
20+
IsMouseVisible = true;
21+
}
22+
23+
protected override void Initialize()
24+
{
25+
// TODO: Add your initialization logic here
26+
27+
base.Initialize();
28+
}
29+
30+
protected override void LoadContent()
31+
{
32+
_spriteBatch = new SpriteBatch(GraphicsDevice);
33+
34+
// Create the texture atlas from the XML configuration file
35+
TextureAtlas atlas = TextureAtlas.FromFile(Content, "images/atlas-definition.xml");
36+
37+
// Create the slime animated sprite
38+
_slime = atlas.CreateAnimatedSprite("slime-animation");
39+
40+
// Create the bat animated sprite
41+
_bat = atlas.CreateAnimatedSprite("bat-animation");
42+
}
43+
44+
protected override void Update(GameTime gameTime)
45+
{
46+
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
47+
Exit();
48+
49+
// Update the slime and bat animated sprites
50+
_slime.Update(gameTime);
51+
_bat.Update(gameTime);
52+
53+
base.Update(gameTime);
54+
}
55+
56+
protected override void Draw(GameTime gameTime)
57+
{
58+
GraphicsDevice.Clear(Color.CornflowerBlue);
59+
60+
_spriteBatch.Begin(samplerState: SamplerState.PointClamp);
61+
62+
// Draw the slime animated sprite
63+
_slime.Draw(_spriteBatch, Vector2.One);
64+
65+
// Draw the bat animated sprite 10px to the right of the slime.
66+
_bat.Draw(_spriteBatch, new Vector2(_slime.Width + 10, 0));
67+
68+
_spriteBatch.End();
69+
70+
base.Draw(gameTime);
71+
}
72+
}

articles/tutorials/building_2d_games/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This documentation will introduce game development concepts using the MonoGame f
2828
| [05: Working with Textures](05_working_with_textures/index.md) | Learn how to load and render textures using the MonoGame content pipeline and [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch). | |
2929
| [06: Optimizing Texture Rendering](06_optimizing_texture_rendering/index.md) | Explore optimization techniques when rendering textures using a texture atlas. | |
3030
| [07: The Sprite Class](07_the_sprite_class/index.md) | Explore creating a reusable Sprite class to efficiently sprites and their rendering properties, including position, rotation, scale, and more. | |
31+
| [08: The AnimatedSprite Class](07_the_sprite_class/index.md) | Create an AnimatedSprite class that builds upon our Sprite class to support frame-based animations. | |
3132

3233
In additional to the chapter documentation, supplemental documentation is also provided to give a more in-depth look at different topics with MonoGame. These are provided through the Appendix documentation below:
3334

0 commit comments

Comments
 (0)