0 votes

extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -250
const FLOOR = Vector2(0, -1)

const ARROW = preload("res://world/Arrow.tscn")

var velocity = Vector2()

var is_attacking = false

var on_ground = false

func physicsprocess(delta):

if Input.is_action_pressed("ui_right"):
    if is_attacking == false:
        velocity.x = SPEED
        $AnimatedSprite.play("Run")
        $AnimatedSprite.flip_h = false
        if sign($Position2D.position.x) == -1:
            $Position2D.position.x *= -1

elif Input.is_action_pressed("ui_left"):
    if is_attacking == false:
        velocity.x = -SPEED
        $AnimatedSprite.play("Run")
        $AnimatedSprite.flip_h = true
        if sign($Position2D.position.x) == 1:
            $Position2D.position.x *= -1


else:
    velocity.x = 0
    if on_ground == true && is_attacking == false:
        $AnimatedSprite.play("Idle")

if Input.is_action_pressed("ui_up"):
    if is_attacking == false:
        if on_ground == true:
            velocity.y = JUMP_POWER
            on_ground = false


if Input.is_action_just_pressed("ui_accept") && is_attacking == false:
    if is_on_floor():
        velocity.x = 0
    is_attacking = true
    $AnimatedSprite.play("Attack")
    var arrow = ARROW.instance()
    if sign($Position2D.position.x) == 1:
        arrow.set_arrow_direction(1)
    else:
        arrow.set_arrow_direction(-1)

    get_parent().add_child(arrow)
    arrow.position = $Position2D.global_position
velocity.y += GRAVITY

if is_on_floor():
    if on_ground == false:
        is_attacking == false
    on_ground = true
else:
    if is_attacking == false:
        on_ground = false
        if velocity.y < 0:
            $AnimatedSprite.play("Jump")

velocity = move_and_slide(velocity, FLOOR)
asked Apr 14 in Engine by Makroman (15 points)

1 Answer

+1 vote
Best answer

If I had to guess it's because In physicsprocess(delta) it runs multiple times per second. So even though you are in the air, you are still holding down the Left or Right key. So it instantly overrides the jump animation with the run animation. Basically.. it did technically work with left and right. It's just it switched back to the running animation so fast you probably didn't see it lol. Maybe try stopping the previous animation until jump animation has finished or until on_ground becomes true again. Sorry I don't have a Code-based answer. Hopefully someone more knowledgeable can give a more direct answer, or you figure it out now. good luck!

Edit: see comment below

This is what I was talking about

if Input.is_action_pressed("ui_right"):
    if is_attacking == false:
        velocity.x = SPEED
        if on_ground == true:
             $AnimatedSprite.play("Run")
             $AnimatedSprite.flip_h = false
        if sign($Position2D.position.x) == -1:
             $Position2D.position.x *= -1
answered Apr 14 by Aireek (112 points)
selected Apr 15 by Makroman

You could try to add a on_ground = true line to your $animatedsprite.play ("run)" line in your movement function. So it will only play that animation if hes on the ground.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.