We have a problem with the animation. The problem is that it won't play the right animation. We are using a spritesheet with 4 frames: idle, movement and so on. Here's the code we are using:
class Animator : Component, IUpdateable
{
private SpriteRenderer spriteRenderer;
private int currentindex;
private float timeElasped;
private float fps;
private Rectangle[] rectangles;
private string animationName;
private Dictionary<string, Animation> animations;
public Animator(GameObject gameObject) : base(gameObject)
{
animations = new Dictionary<string, Animation>();
fps = 5;
spriteRenderer = (SpriteRenderer)gameObject.GetComponent("SpriteRenderer");
}
public void Update()
{
timeElasped += GameWorld.Instance.deltaTime;
currentindex = (int)(timeElasped * fps);
if (currentindex > rectangles.Length - 1)
{
GameGetGameObject.OnAnimationDone(animationName);
timeElasped = 0;
currentindex = 0;
}
spriteRenderer.Rectangle = rectangles[currentindex];
}
public void CreateAnimation(string name, Animation animation)
{
animations.Add(name, animation);
}
public void PlayAnimation(string animationName)
{
if (this.animationName != animationName)
{
this.rectangles = animations[animationName].GetRectangle;
this.spriteRenderer.Rectangle = rectangles[0];
this.spriteRenderer.Offset = animations[animationName].GetOffSet;
this.animationName = animationName;
this.fps = animations[animationName].GetFps;
timeElasped = 0;
currentindex = 0;
}
}
}
That's part of the animation code. Next is the player class that uses the animation code.
class Player : Component, IUpdateable, ILoadable, IAnimateable
{
private float speed = 50;
public Player(GameObject gameObject) : base(gameObject)
{
}
public Animator Animator
{
get { return (Animator)GameGetGameObject.GetComponent("Animator"); }
}
public void Update()
{
Move();
}
public void Move()
{
Vector2 translation = Vector2.Zero;
KeyboardState KeyState = Keyboard.GetState();
if (KeyState.IsKeyDown(Keys.W))
{
translation += new Vector2(0, -1);
Animator.PlayAnimation("MovingZero");
Animator.PlayAnimation("MovingOne");
Animator.PlayAnimation("MovingTwo");
}
if (KeyState.IsKeyDown(Keys.S))
{
translation += new Vector2(0, 1);
Animator.PlayAnimation("MovingZero");
Animator.PlayAnimation("MovingOne");
Animator.PlayAnimation("MovingTwo");
}
if (KeyState.IsKeyDown(Keys.A))
{
translation += new Vector2(-1, 0);
Animator.PlayAnimation("MovingZero");
Animator.PlayAnimation("MovingOne");
Animator.PlayAnimation("MovingTwo");
}
if (KeyState.IsKeyDown(Keys.D))
{
translation += new Vector2(1, 0);
Animator.PlayAnimation("MovingZero");
Animator.PlayAnimation("MovingOne");
Animator.PlayAnimation("MovingTwo");
}
Animator.PlayAnimation("MovingZero");
GameGetGameObject.GetTransform().Translate(translation * GameWorld.Instance.deltaTime * speed);
}
public void CreateAnimations()
{
Animator.CreateAnimation("Idle",new Animation(4,0,0,42,32,4,Vector2.Zero));
Animator.CreateAnimation("MovingZero", new Animation(4, 0, 1, 42, 32, 4, Vector2.Zero));
Animator.CreateAnimation("MovingOne", new Animation(4, 0, 2, 42, 32, 4, Vector2.Zero));
Animator.CreateAnimation("MovingTwo", new Animation(4, 0, 3, 42, 32, 4, Vector2.Zero));
}
public void LoadContent(ContentManager Content)
{
CreateAnimations();
Animator.PlayAnimation("Idle");
}
public void OnAnimationDone(string animationName)
{
}
}
We are using the component-pattern. This is our animation component:
class Animation : Component
{
private float Fps;
private Vector2 Offset;
private Rectangle[] rectangles;
public float GetFps { get; set; }
public Vector2 GetOffSet { get; set; }
public Rectangle[] GetRectangle
{
get { return rectangles; }
set { rectangles = value; }
}
public Animation(int frames, int yPos,int xStratFrame, int width, int height, float fps, Vector2 offset)
{
rectangles = new Rectangle[frames];
Offset = offset;
Fps = fps;
for (int i = 0; i < frames; i++)
{
rectangles[i] = new Rectangle((i + xStratFrame) * width,yPos,width,height);
}
}
}
And finally the code for the sprite renderer:
class SpriteRenderer : Component, ILoadable, IDrawable
{
private GameObject gameObject;
public Texture2D sprite;
private string spriteName;
private float layerDepth;
public Vector2 Offset { get; set; }
public Rectangle Rectangle { get; set; }
public SpriteRenderer(GameObject gameObject, string spriteName, float layerDepth)
{
this.gameObject = gameObject;
this.spriteName = spriteName;
this.layerDepth = layerDepth;
}
public void Update()
{
Update();
}
public void LoadContent(ContentManager content)
{
this.sprite = content.Load<Texture2D>(spriteName);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(sprite, gameObject.GetTransform().GetPosition(), Rectangle, Color.White);
}
}
Basically the player can move and it plays a part of the animation but not all of it. How do I fix the animation playback?