Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Noth |
I have a problem with my animation. The program works but when I press left and right at the same time or up and down one of the two wins on the other. This is the code:
extends KinematicBody2D
func _ready():
pass
func _process(delta):
var velocity = Vector2()
if Input.is_key_pressed(KEY_UP):
velocity.y = -1
if Input.is_key_pressed(KEY_DOWN):
velocity.y = +1
if Input.is_key_pressed(KEY_RIGHT):
velocity.x = +1
if Input.is_key_pressed(KEY_LEFT):
velocity.x = -1
var movement = velocity.normalized()*500*delta
self.move_and_collide(movement)
self.animations(velocity)
func animations(velocity):
if velocity.y == -1:
$AnimatedSprite.play("walk_up")
if velocity.y == 1:
$AnimatedSprite.play("walk_down")
if velocity.x == -1:
$AnimatedSprite.play("walk_left")
if velocity.x == 1:
$AnimatedSprite.play("walk_right")
if velocity == Vector2():
if $AnimatedSprite.animation == 'walk_up':
$AnimatedSprite.play("stand_up")
elif $AnimatedSprite.animation == 'walk_down':
$AnimatedSprite.play("stand_down")
elif $AnimatedSprite.animation == 'walk_left':
$AnimatedSprite.play("stand_left")
elif $AnimatedSprite.animation == 'walk_right':
$AnimatedSprite.play("stand_right")