0
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

you reset the animation each time

    }

   Animator.PlayAnimation("MovingZero");
    GameGetGameObject.GetTransform().Translate(translation * GameWorld.Instance.deltaTime * speed);

}

and

        Animator.PlayAnimation("MovingZero");
        Animator.PlayAnimation("MovingOne");
        Animator.PlayAnimation("MovingTwo");

won't work. you have to wait to finish the animation first or all statements will be called first, then the last will be rendered

monogame have 2 loops, the draw and update loop. you need a counter that counts the frames, each frame you have to increment it and check which frame now should be rendered.

\$\endgroup\$
9
  • \$\begingroup\$ ehm so what exactly do u mean? i am honestly a big noob at monogame we had one week of learning basicly so i am abit bronze on this spot. is there a chance u could show how? \$\endgroup\$ Feb 12, 2016 at 16:46
  • \$\begingroup\$ if you upload it, I could try to fix it :) \$\endgroup\$
    – EnemyArea
    Feb 12, 2016 at 16:57
  • \$\begingroup\$ let me upload it \$\endgroup\$ Feb 13, 2016 at 17:31
  • \$\begingroup\$ if u like to help us here is the full project <a filedropper.com/giftman \$\endgroup\$ Feb 13, 2016 at 17:37
  • \$\begingroup\$ dropbox.com/s/n62q4ovn0jhxqlv/giftmanFixed.zip?dl=0 \$\endgroup\$
    – EnemyArea
    Feb 13, 2016 at 19:29

Not the answer you're looking for? Browse other questions tagged .