![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | funny_man |
Hello guys! Im new to godot so sorry if this question is stupid.
My problem is that when i move on StaticBody2d (platform) AnimatedSprite playing both move and fall animations at the same time. My code:
extends KinematicBody2D
export var is_falling: bool
func _process(delta: float) -> void:
is_falling = !is_on_floor()
move_and_slide(player_move(delta, is_falling), Vector2.UP)
func player_move(delta: float, is_falling: bool) -> Vector2:
if !is_falling:
var move_direction: Vector2
if Input.is_action_pressed("ui_right") and not Input.is_action_pressed("ui_left"):
move_direction.x = 50
player_move_animation("right")
elif Input.is_action_pressed("ui_left") and not Input.is_action_pressed("ui_right"):
move_direction.x = -50
player_move_animation("left")
else:
move_direction.x = 0
player_move_animation("stop")
return move_direction
else:
var move_direction: Vector2
if Input.is_action_pressed("ui_right") and not Input.is_action_pressed("ui_left"):
move_direction.x = 50
player_fall(delta, "right")
elif Input.is_action_pressed("ui_left") and not Input.is_action_pressed("ui_right"):
move_direction.x = -50
player_fall(delta, "left")
else:
move_direction.x = 0
player_fall(delta, "stop")
return move_direction
func player_move_animation(var move_direction: String) -> void:
if move_direction == "right":
$PlayerTexture.flip_h = true
$PlayerTexture.play("move")
if move_direction == "left":
$PlayerTexture.flip_h = false
$PlayerTexture.play("move")
$PlayerTexture.playing = true
if move_direction == "stop":
$PlayerTexture.play("idle")
else:
return
func player_fall(delta: float, var fall_direction: String) -> void:
position.y += 100 * delta
player_fall_animation(fall_direction)
func player_fall_animation(var fall_direction: String) -> void:
if fall_direction == "right":
$PlayerTexture.flip_h = true
$PlayerTexture.play("fall")
if fall_direction == "left":
$PlayerTexture.flip_h = false
$PlayerTexture.play("fall")
else:
return
I don’t know where your exact problem lies (maybe some variables are “true” at the same time?). What I would suggest is touching up some of those animation functions with some match
statements:
func player_move_animation(var move_direction: String) -> void:
match move_direction:
"right":
$PlayerTexture.flip_h = true
$PlayerTexture.play("move")
"left":
$PlayerTexture.flip_h = false
$PlayerTexture.play("move")
$PlayerTexture.playing = true
"stop":
$PlayerTexture.play("idle")
_:
return
func player_fall_animation(var fall_direction: String) -> void:
match fall_direction:
"right":
$PlayerTexture.flip_h = true
$PlayerTexture.play("fall")
"left":
$PlayerTexture.flip_h = false
$PlayerTexture.play("fall")
_:
return
Hope these changes make your code run better.
Ertain | 2021-06-18 16:57