Changing animation of an AI

Hi, so I’ll get right into it. Let’s say I have 4 animations, one for each direction, upAnim, downAnim, leftAnim and rightAnim. I have
code that will move my AI sprite towards the player, but how do I tell it which animation should be used? I tried something like


if(vel.x < 0){
    //sprite is moving left
    setAnim(leftAnim);
}else{
    setAnim(rightAnim);
}

However, it get’s tricky with all four directions, and the sprite will switch animations rapidly at the wrong times.

What’s the best way to get animations to change based on the velocity of the sprite?