Help Animation

BR__BR__ Posts: 1Member
edited July 19 in 2D

Hello, I'm a beginner in programming and godot, and I'm looking to learn how to develop games on this engine, anyway, watching some classes I tried to start making a 2D game but I'm having problems in the animation part, following the tutorial the following problem happened : every time the character performed the 'jump' animation, only the first frame was executed. I wanted to know how to make him execute all the frames jumping and falling, it can be something very silly but I'm having real difficulties

code:

extends KinematicBody2D

const UP = Vector2(0, -1)
const SPEED = 200
const GRAVITY = 20
const JUMP_HEIGHT = -450 
var motion = Vector2()

func _physics_process(delta):

    motion.y += GRAVITY


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

    if is_on_floor():
        if Input.is_action_pressed("ui_up"):
            motion.y = JUMP_HEIGHT
    else:
        $AnimatedSprite.play("jump")

    motion = move_and_slide(motion, UP)

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,831Admin

    Welcome to the forums @BR__!

    I don't know for sure and I only looked through the code quickly, but the issue is likely that the animation is being overridden before it has a chance to play, since the play function is being called without checking to see if the animation already playing is the desired animation.
    To fix this, you can make a function like this:

    func change_animation(new_animation):
        if $AnimatedSprite.animation == new_animation:
            return # do nothing!
        # otherwise, play the animation
        $AnimatedSprite.play(new_animation)
    

    Then, instead of using $AnimatedSprite.play, you can instead use change_animation. For example, you could replace line 30 to change_animation("jump") and that should fix the issue you are having with the animation.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file