Sprite idle animation

Hey guys i am trying to make my 2d character (Sprite) to Idle in a certain direction

So first i just wanted it to just idle when there was no input so i made this:

if (Velocity.X == 0)
_animationManager.Play(_animations[“WalkIdle”]);

Thats what i added to my other code i already had just to walk:

if (Velocity.X > 0)
_animationManager.Play(_animations[“WalkRight”]);
else if (Velocity.X < 0)
_animationManager.Play(_animations[“WalkLeft”]);
else if (Velocity.Y > 0)
_animationManager.Play(_animations[“WalkDown”]);
else if (Velocity.Y < 0)
_animationManager.Play(_animations[“WalkUp”]);

But now my question is How can i let it idle a certain direction??
this is what i made but i didnt work and it just displayed the IdleDown animation the whole time:

if (Velocity.X > 0)
_animationManager.Play(_animations[“WalkRight”]);
else if (Velocity.X < 0)
_animationManager.Play(_animations[“WalkLeft”]);
else if (Velocity.Y > 0)
_animationManager.Play(_animations[“WalkDown”]);
else if (Velocity.Y < 0)
_animationManager.Play(_animations[“WalkUp”]);
else if (Velocity.Y <= 0)
_animationManager.Play(_animations[“WalkIdleDown”]);
else if (Velocity.Y >= 0)
_animationManager.Play(_animations[“WalkIdleUp”]);
else if (Velocity.X <= 0)
_animationManager.Play(_animations[“WalkIdleRight”]);
else if (Velocity.X >= 0)
_animationManager.Play(_animations[“WalkIdleLeft”]);

What am i doing Wrong??

You already covered the cases where Velocity.X < 0, Velocity.X > 0, Velocity.Y < 0 and Velocity.Y > 0, so when you get to this part of the code you know that Velocity.X == 0 and Velocity.Y == 0. Because of that you will always use the WalkIdleDown animation. To fix this, you should remember what direction the player moved in last. An example of how you could approach this is the following:

if (Velocity.X > 0)
{
    animationManager.Play(animations["WalkRight"]);
    lastDir = Direction.Right;
}
else if (Velocity.X < 0)
{
    animationManager.Play(animations["WalkLeft"]);
    lastDir = Direction.Left;
}
else if (Velocity.Y > 0)
{
    animationManager.Play(animations["WalkDown"]);
    lastDir = Direction.Down;
}
else if (Velocity.Y < 0)
{
    animationManager.Play(animations["WalkUp"]);
    lastDir = Direction.Up;
}
else // velocity is (0, 0) => we should play an idle animation for the last direction
{
    // the $ { } notations does string interpolation, that means the stuff in between the braces
    // is replaced by the value of what's in it, so the below string could be e.g. "WalkIdleRight" if
    // lastDir is equal to Direction.Right
    animationManager.Play(animation[$"WalkIdle{lastDir.ToString()}"]);
}

where Direction is an enum that looks like this:

enum Direction
{
    Right,
    Left,
    Down,
    Up
}

I recommend you follow some programming tutorials to help you understand things better. Getting a better grasp of programming in general will help you solve issues like this more easily.

First of all if i copy that in i get this error list…

This is the code now:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Survival2D.Managers;
using Survival2D.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Survival2D.Sprites
{
public class Sprite
{
#region Fields

    protected AnimationManager _animationManager;

    protected Dictionary<string, Animation> _animations;

    protected Vector2 _position;

    protected Texture2D _texture;

    #endregion

    #region Properties

    public Input Input;

    public Vector2 Position
    {
        get { return _position; }
        set
        {
            _position = value;

            if (_animationManager != null)
                _animationManager.Position = Position;
        }
    }

    public float Speed = 2f;

    public Vector2 Velocity;

    #endregion

    #region Method

    public virtual void Draw(SpriteBatch spriteBatch)
    {
        if (_texture != null)
            spriteBatch.Draw(_texture, Position, Color.White);
        else if (_animationManager != null)
            _animationManager.Draw(spriteBatch);
        else throw new Exception("This ain't right..!");

    }

    public virtual void Move()
    {
        if (Keyboard.GetState().IsKeyDown(Input.Up))
            Velocity.Y = -Speed;
        if (Keyboard.GetState().IsKeyDown(Input.Down))
            Velocity.Y = Speed;
        if (Keyboard.GetState().IsKeyDown(Input.Left))
            Velocity.X = -Speed;
        if (Keyboard.GetState().IsKeyDown(Input.Right))
            Velocity.X = Speed;
    }
    enum Direction
    {
        Right,
        Left,
        Down,
        Up
    }
    protected virtual void SetAnimations()
    {
        if (Velocity.X > 0)
        {
            animationManager.Play(animations["WalkRight"]);
            lastDir = Direction.Right;
        }
        else if (Velocity.X < 0)
        {
            animationManager.Play(animations["WalkLeft"]);
            lastDir = Direction.Left;
        }
        else if (Velocity.Y > 0)
        {
            animationManager.Play(animations["WalkDown"]);
            lastDir = Direction.Down;
        }
        else if (Velocity.Y < 0)
        {
            animationManager.Play(animations["WalkUp"]);
            lastDir = Direction.Up;
        }
        else // velocity is (0, 0) => we should play an idle animation for the last direction
        {
            // the $ { } notations does string interpolation, that means the stuff in between the braces
            // is replaced by the value of what's in it, so the below string could be e.g. "WalkIdleRight" if
            // lastDir is equal to Direction.Right
            animationManager.Play(animation[$"WalkIdle{lastDir.ToString()}"]);
        }

    public Sprite(Dictionary<string, Animation> animations)
    {
        _animations = animations;
        _animationManager = new AnimationManager(_animations.First().Value);
    }

    public Sprite(Texture2D texture)
    {
        _texture = texture;
    }

    public virtual void Update(GameTime gameTime, List<Sprite> sprites)
    {
        Move();

        SetAnimations();

        _animationManager.Update(gameTime);

        Position += Velocity;
        Velocity = Vector2.Zero;
    }

    #endregion
}

}

i also did not know where to put:

enum Direction
{
Right,
Left,
Down,
Up
}

and i also dont understand where “LastDir” comes from because its not a variable or something
btw im just starting and learning mono game and c# I gave you my whole code could you fix it and send me it how i should do it because i learn alot more of looking in to code that someone made for me and change some things then just trying myself because then i fuck up my whole code

Enumeration is pretty basic stuff. Just syntactic flavoring, you could just as easily use 0, 1, 2, and 3 and store it in an int but enumeration just makes it more readable. I suggest you google it, I’m certain there’s lots of tutorials for it out there.

The (logical) problem you have is when you’re not moving - when you’re no longer receiving user input - you need to know which direction you last moved in. So you need to make your program remember, by putting it in a (persistent) variable. So in your Sprite class you simply put
protected Direction lastDir;
or if you prefer to avoid enumeration you could do
protected int lastDir;
You can put this in your Fields region (or anywhere inside the Sprite class, really). Same goes for the enum, or just putting the enum in the namespace works too.

One example of an enum tutorial.

Although fully functional examples aren’t a bad way to learn, the best way is to try it out. Start out with your initial code, and add an enum without getting errors. Then add a variable. Then set the variable. Then use the variable. Etc…