-
Notifications
You must be signed in to change notification settings - Fork 0
MonoGame Module

This section explains the installation process and use of the MonoGame module of the Library.
There are many ways to install the package, here are some:
Open your terminal of choice and past the following command:
dotnet add package AsepriteImporter.MG
On Visual Studio, open the Package Manager Console
.
Tools > NuGet Package Manager > Package Manager Console
And paste the following command:
NuGet\Install-Package AsepriteImporter.MG
Edit your project file (.csproj
) and paste this line:
<PackageReference Include="AsepriteImporter.MG"/>
The AsepriteAnimator
class can be used to handle animations of a sprite sheet.
To create an instance, it requires loaded AsepriteSheet (more about it on the "Core" page), and a counter object.
To allow a more flexible implementation of counting up the frames, and not tie it to MonoGame's IGameComponent
system, the animator uses an instance of a object that implements the IAnimationCounter
interface.
You can skip implementing the IAnimationCounter
interface by using one of two(2) built-in counters. Both work with MonoGame's IGameComponent
system, and implement the IDrawable
interface, therefore, you must provide either the main Game
instance, or its GameComponentCollection
reference.
Both set their DrawOrder
to int.MinValue
, so they run before any other drawable component, but if needed, you can provide a custom drawOrder
, as an optional parameter.
- DrawTimeCounter: counts the amount of milliseconds passed since last frame.
-
DrawFrameCounter: counts up (+1) for every time its
Draw(GameTime)
method is called.
ⓘ QUICK INFO Generally, you want to use the
DrawTimeCounter
, since Aseprite stores frame times as milliseconds.
EXAMPLE:
private IAnimationCounter GetCounter(Game game)
{
return new DrawTimeCounter(game);
}
The only required method in the IAnimationCounter
interface, is the GetCount()
method, whose purpose is to provide the current count of the counter. The way your counter actually increases, is up to you.
EXAMPLE:
public class MyCounter : IAnimationCounter
{
private double _count;
public double GetCount()
{
// Counts up every time GetCount() is requested
_count += 1;
return _count;
}
}
The animator itself doesn't Draw the texture, rather, you call the GetFrame(string)
method, passing in the name
of the animation, which returns a Rectangle
representing the area on the texture corresponding to the current frame.
Since the animator uses a single counter, it doesn't matter how many times you call GetFrame(string)
repeatedly, the frame will wrap based on the total duration of the animation.
EXAMPLE:
private void Draw(GameTime gameTime)
{
Rectangle playerFrame = animator.GetFrame("player.idle");
Rectangle monsterFrame = animator.GetFrame("monster.idle");
spriteBatch.Begin();
spriteBatch.Draw(texture, position, playerFrame, Color.White);
spriteBatch.Draw(texture, position, monsterFrame, Color.White);
spriteBatch.End();
}
To go more in depth about all the MonoGame library's methods and classes, you may find the full documentation here.
By Varollo.