0 votes

Hi! I'm new in godot and in programming and i don't know why the sprites don't appear any advice will be great.
Thank you for you answer!

Here's the code:

const SPEED = 350
const GRAVITY = 40
const JUMP = -1100
const FLOOR = Vector2(0, -1)

var velocity = Vector2()
var jumping = false
var falling = false

func physicsprocess(delta: float) -> void:

if Input.is_action_pressed("ui_right"):
    velocity.x = SPEED
    $AnimatedSprite.play("run")
    $AnimatedSprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
    velocity.x = -SPEED
    $AnimatedSprite.play("run")
    $AnimatedSprite.flip_h = true
else:
    velocity.x = 0
    $AnimatedSprite.play("idle")

if is_on_floor():
    if not jumping and not falling:
        if Input.is_action_pressed("ui_up"):
            velocity.y = JUMP
            jumping = true
            if jumping:
                $AnimatedSprite.play("jump")
                jumping = false
                falling = true
                if falling:
                    $AnimatedSprite.play("jump")
                    falling = false

velocity.y += GRAVITY

velocity = move_and_slide(velocity, FLOOR)
in Engine by (12 points)

1 Answer

0 votes

As far as i can see, every frame you set either one of this animations: run or idle
After that, you may change that animation to fall or jump, but as you change it every frame (because previously you set run or idle) animation is restarted everey frame when you jump or fall.

Try something like this instead (although you will probably need to think about if that implementation is right for your use case):

if is_on_floor():
    if not jumping and not falling:
        if Input.is_action_pressed("ui_up"):
            velocity.y = JUMP
            jumping = true
            if jumping:
                $AnimatedSprite.play("jump")
                jumping = false
                falling = true
                if falling:
                    $AnimatedSprite.play("jump")
                    falling = false
elif Input.is_action_pressed("ui_right"):
    velocity.x = SPEED
    $AnimatedSprite.play("run")
    $AnimatedSprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
    velocity.x = -SPEED
    $AnimatedSprite.play("run")
    $AnimatedSprite.flip_h = true
else:
    velocity.x = 0
    $AnimatedSprite.play("idle")

velocity.y += GRAVITY

velocity = moveandslide(velocity, FLOOR)

by (3,491 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.