Problem trying to animate a Sprite

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

I’m trying to animate a sprite that needs ot move left an right, but when I try it, it doesn’t move. Here is the code that I’m using:

extends KinematicBody2D

export (int) var v_walk = 100

var speed = Vector2()

func get_input():
speed.x = 0
var right = Input.is_action_pressed(‘ui_right’)
var left = Input.is_action_pressed(“ui_left”)

if right:
	speed.x += v_walk
	$AnimatedSprite.flip_h = speed.x < 0
	$AnimatedSprite.animation = "Walk"
elif left:
	speed.x -= v_walk
	$AnimatedSprite.flip_h = speed.x < 0
	$AnimatedSprite.animation = "Walk"
else:
	$AnimatedSprite.animation = "Idle"

If someone could help out It would be appreciated! :slight_smile:

:bust_in_silhouette: Reply From: DONOTJUDGEMYCODE

It looks like here you’re setting a speed variable but never actually using it. Try adding a move_and_slide function and pass in speed.

Two grammar mistakes; isactionpressed should be is_action_pressed, and uiright should be ui_right.

You should also replace getinput() with _process(_delta) as there is no such function, and if you meant for it to be a custom function, there is no code that calls it.

I believe that this would be the proper code:

export (int) var v_walk = 100

var speed = Vector2()

func _process(_delta):
    speed.x = 0
    var right = Input.is_action_pressed('ui_right')
    var left = Input.is_action_pressed("ui_left")

    if right:
        speed.x += v_walk
        $AnimatedSprite.flip_h = speed.x < 0
        $AnimatedSprite.animation = "Walk"
    elif left:
        speed.x -= v_walk
        $AnimatedSprite.flip_h = speed.x < 0
        $AnimatedSprite.animation = "Walk"
    else:
        $AnimatedSprite.animation = "Idle"

func _physics_process(_delta):
    move_and_slide(speed)