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
$Animak.play(“walk_down”)

elif Input.is_action_pressed("up"):
	move.y = -speed
	$Animak.play("walk_up")
	#When the keys are released, the player's movement stops
else:
	move.y = 0
	#Press key to move "left" and "right"
if Input.is_action_pressed("left"):
	move.x = -speed
	$Animak.flip_h = false
	$Animak.play("walk_left")
	
elif Input.is_action_pressed("right"):
	move.x = speed
	$Animak.flip_h = true
	$Animak.play("walk_left")
	#Stopping a player after releasing the keys and putting him in the "idle" position
else:
	move.x = 0
if Input.is_action_just_released("left"):
	$Animak.play("idle")
if Input.is_action_just_released("right"):
	$Animak.play("idle")
if Input.is_action_just_released("down"):
	$Animak.play("idle")
if Input.is_action_just_released("up"):
	$Animak.play("idle")
	
	#Combination by pressing two keys
if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
	$Animak.flip_h = false
	$Animak.play("walk_up_right")
if Input.is_action_pressed("left") and Input.is_action_pressed("up"):
	$Animak.flip_h = true
	$Animak.play("walk_up_right")
if Input.is_action_pressed("right") and Input.is_action_pressed("down"):
	$Animak.flip_h = false
	$Animak.play("walk_down_right")
if Input.is_action_pressed("left") and Input.is_action_pressed("down"):
	$Animak.flip_h = true
	$Animak.play("walk_down_right")
	
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