I have a class named Animation which handles my animations. The animation class can be called from multiple other classes. For example, the class Player.cs can call the animation class like this:
Animation Playeranimation;
Playeranimation = new Animation(TimeSpan.FromSeconds(2.5f), 80, 40, Animation.Sequences.forwards, 0, 5, false, true);
//updating the animation
public void Update(GameTime gametime)
{
Playeranimation.Update(gametime);
}
//drawing the animation
public void Draw(SpriteBatch batch)
{
playeranimation.Draw(batch, PlayerAnimationSpritesheet, PosX, PosY, 0, SpriteEffects.None);
}
The class Lion.cs can call the animation class with the same code, only the animation parameters are changing because it's another animation that should be played:
Animation Lionanimation;
Lionanimation = new Animation(TimeSpan.FromSeconds(2.5f), 100, 60, Animation.Sequences.forwards, 0, 8, false, true);
Other classes can call the animation class with the same code like the Player class.
But sometimes I have some trouble with the animations. If an animation is running and then shortly afterwards another class calls the animation class too, the second animation starts but the first animation is getting aborted. In this case, the first animation couldn't run until it's end because another class started a new instance of the animation class.
Why is an animation sometimes getting aborted when another animation starts?
How can I solve this problem?
My animation class:
public class Animation
{
private int _animIndex, framewidth, frameheight, start, end;
private TimeSpan PassedTime;
private List<Rectangle> SourceRects = new List<Rectangle>();
private TimeSpan Duration;
private Sequences Sequence;
public bool Remove;
private bool DeleteAfterOneIteration;
public enum Sequences
{
forwards, backwards, forwards_backwards, backwards_forwards
}
private void forwards()
{
for (int i = start; i < end; i++)
SourceRects.Add(new Rectangle(i * framewidth, 0, framewidth, frameheight));
}
private void backwards()
{
for (int i = start; i < end; i++)
SourceRects.Add(new Rectangle((end - 1 - i) * framewidth, 0, framewidth, frameheight));
}
private void forwards_backwards()
{
for (int i = start; i < end - 1; i++)
SourceRects.Add(new Rectangle(i * framewidth, 0, framewidth, frameheight));
for (int i = start; i < end; i++)
SourceRects.Add(new Rectangle((end - 1 - i) * framewidth, 0, framewidth, frameheight));
}
private void backwards_forwards()
{
for (int i = start; i < end - 1; i++)
SourceRects.Add(new Rectangle((end - 1 - i) * framewidth, 0, framewidth, frameheight));
for (int i = start; i < end; i++)
SourceRects.Add(new Rectangle(i * framewidth, 0, framewidth, frameheight));
}
public Animation(TimeSpan duration, int frame_width, int frame_height, Sequences sequences, int start_interval, int end_interval, bool remove, bool deleteafteroneiteration)
{
Remove = remove;
DeleteAfterOneIteration = deleteafteroneiteration;
framewidth = frame_width;
frameheight = frame_height;
start = start_interval;
end = end_interval;
switch (sequences)
{
case Sequences.forwards:
{
forwards();
break;
}
case Sequences.backwards:
{
backwards();
break;
}
case Sequences.forwards_backwards:
{
forwards_backwards();
break;
}
case Sequences.backwards_forwards:
{
backwards_forwards();
break;
}
}
Duration = duration;
Sequence = sequences;
}
public void Update(GameTime dt)
{
PassedTime += dt.ElapsedGameTime;
if (PassedTime > Duration)
{
PassedTime -= Duration;
}
var percent = PassedTime.TotalSeconds / Duration.TotalSeconds;
if (DeleteAfterOneIteration == true)
{
if (_animIndex >= SourceRects.Count)
Remove = true;
_animIndex = (int)Math.Round(percent * (SourceRects.Count));
}
else
{
_animIndex = (int)Math.Round(percent * (SourceRects.Count - 1));
}
}
public void Draw(SpriteBatch batch, Texture2D Textures, float PositionX, float PositionY, float Rotation, SpriteEffects Flip)
{
if (DeleteAfterOneIteration == true)
{
if (_animIndex >= SourceRects.Count)
return;
}
batch.Draw(Textures, new Rectangle((int)PositionX, (int)PositionY, framewidth, frameheight), SourceRects[_animIndex], Color.White, Rotation, new Vector2(framewidth / 2.0f, frameheight / 2.0f), Flip, 0f);
}
}