Animations
The MonoGame.Extended.Animations library contains classes useful for animating 2D sprite.
Installation
MonoGame.Extended.Animations is distributed via a NuGet package. You can add the NuGet package to your C# project through your IDE of choice (Visual Studio, Xamarin Studio, Rider, etc) or through the Command Line Interface (CLI) using the dotnet command.
Setup SpriteFactory
In this example we use SpriteFactory for animating Sprites. we are going to build it ourselves from the source.
Step 1 clone the SpriteFactory repository
git clone https://github.com/craftworkgames/SpriteFactory.git
Step 2 Build and run
cd SpriteFactory
dotnet run --project SpriteFactory
Usage
Creating the Animation
info
The assets used in this example can be downloaded here
Before we can load an animation, we must first animate it in SpriteFactory.
We are using the mowt.png
from the assets
Step 1
Create new project in File -> New
Step 2
Open mowt.png in Texture
Step 3 Set Tile Size to 52 by 72 pixels
Step 4 Add your animations and configure your prefered Frame duration
Next we save our animation as mowt.sf
ContentPipeline
SpriteFactory animation does not have to be imported with the ContentPipeline
But we de need to tell the compiler to copy mowt.sf
to the output folder.
In visual studio you can do that by clicking on mowt.sf
in the Solution Explorer and setting Copy to Output Directorty
to Copy if newer
.
You can also set this in your project.csproj
by adding the following
motw.png
can just be imported as Texture2D.
Loading the Animation
We start by defining our fields
Next we load the content
To use new JsonContentLoader()
you need to use the Content.Load extension
Updating the Animation
We use the following code to update the Position and Animation of the Player.