Script not play the entire animated sprite (the only one frame is shown), after I press key "d" and "w".

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

extends KinematicBody2D

export var speed = 50
var move = Vector2()

func _physics_process(_delta):
#Press key to move “down” and “up”
if Input.is_action_pressed(“down”):
move.y = speed

elif Input.is_action_pressed("up"):
	move.y = -speed
	#When the keys are released, the player's movement stops
	move.y = 0
	#Press key to move "left" and "right"
if Input.is_action_pressed("left"):
	move.x = -speed
	$Animak.flip_h = false
elif Input.is_action_pressed("right"):
	move.x = speed
	$Animak.flip_h = true
	#Stopping a player after releasing the keys and putting him in the "idle" position
	move.x = 0
if Input.is_action_just_released("left"):
if Input.is_action_just_released("right"):
if Input.is_action_just_released("down"):
if Input.is_action_just_released("up"):
	#Combination by pressing two keys
if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
	$Animak.flip_h = false
if Input.is_action_pressed("left") and Input.is_action_pressed("up"):
	$Animak.flip_h = true
if Input.is_action_pressed("right") and Input.is_action_pressed("down"):
	$Animak.flip_h = false
if Input.is_action_pressed("left") and Input.is_action_pressed("down"):
	$Animak.flip_h = true
move = move_and_slide(move, Vector2(0,-1))

Show that entire function. The problem is probably in that function, but not in the code you show. You’re probably starting another animation unexpectedly.

Also, after you paste your code to the forum, select it and press the { } button to properly format it.

jgodfrey | 2020-11-08 19:27

Thank you for your comment.

Bran_coder | 2020-11-10 16:07

Jgodfrey, thank you for your comment. I uploaded the entire code.

Bran_coder | 2020-11-11 11:17

:bust_in_silhouette: Reply From: jgodfrey

Looking at the code, I’d guess the problem lies in the fact that the various key combinations are being processed in a set of independent if blocks.

For example, if up and right are pressed, the code will first see that the up key is pressed and start the walk_up animation. Later, it’ll recognize that both up and right are pressed and start the walk_up_right animation.

Assuming the keys are held down until the next frame, it’ll do the same thing again. So, it will cycle between the first frames of several different animations.

To resolve that, you need to process all of the key combinations in a single if or match block that determines the one, correct animation to play based on the current set of pressed keys.

Hi Jgodfrey, what do you mean “only one single if”? Can you show me how, please?
Thank you.

Bran_coder | 2020-11-12 16:48

Is your project available (or, can it be made available) somewhere for review?

jgodfrey | 2020-11-12 17:00

No, my project is not available. It is only in my laptop, on my hard drive.

Bran_coder | 2020-11-15 18:17