Animation plays every tick from the start

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AfterCadaver

Animation Problems

I have been following fornclake’s Zelda clone tutorials on youtube. After initially achieving the correct results in one project, every attempt to repeat the same has failed.

My problem is that seemingly when I try to switch animations on movement, the animation plays every tick, resulting in it appearing to be frozen as the object moves on screen.

What do i need to do to fix this and animate properly?

This is not the result of an error or anything. I have just been unable to fix this poroblem.

extends KinematicBody2D

const SPEED = 32
  var movedir = Vector2(0,0)
    
    var spritedir = 'down'
    
    func _physics_process(delta):
        controls_loop()
        movement_loop()
        spritedir_loop()
        
        if movedir != Vector2(0,0):
            anim_switch('walk')
        else:
            anim_switch('idle')
    
    func controls_loop():
        var left  = Input.is_action_pressed('ui_left')
        var right = Input.is_action_pressed('ui_right')
        var up    = Input.is_action_pressed('ui_up')
        var down  = Input.is_action_pressed('ui_down')
    
        movedir.x = -int(left) + int(right) 
        movedir.y = -int(up)   + int(down)
    
    func movement_loop():
    var motion = movedir.normalized() * SPEED
    move_and_slide(motion, Vector2(0,0))

func spritedir_loop():
    match movedir:
        Vector2(-1,0):
            spritedir = 'left'
        Vector2(1,0):
            spritedir = 'right'
        Vector2(0,-1):
            spritedir = 'up'
        Vector2(0,1):
            spritedir = 'down'

func anim_switch(animation): 
    var newanim = str(animation,spritedir)
    if $anim.current_animation != newanim:
        $anim.play(newanim)

hi, you posted this twice! you can hide this question and continue with the other, so we don have duplicates!.

p7f | 2019-01-22 14:06