trying to make a simple 2d game but the slide and jump animations only plays the first frame

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mbarake

trying to make a simple platformer but the slide and jump animations only play the first frame, i tried every thing (associating the animations with variables and other things and i keep running into the same problem, godot only plays the first frame, however i dont have that problem with the run animation. here is my code)

extends KinematicBody2D

const UP = Vector2(0,-1)
const GRAVITY = 20
const SPEED = 300
const JUMP_HEIGHT = -700

var velocity = Vector2()
var climbing = false;
var is_jumping = false

func _process(delta):
velocity.y += GRAVITY

if Input.is_action_pressed("right"):
	velocity.x = SPEED
	$AnimatedSprite.play("run")
	$AnimatedSprite.flip_h = false

elif Input.is_action_pressed("left"):
	velocity.x = -SPEED
	$AnimatedSprite.play("run")
	$AnimatedSprite.flip_h = true

if Input.is_action_pressed("left"):
	if Input.is_action_pressed("down"):
		velocity.x = -SPEED * 2
		$AnimatedSprite.play("slide")
		$standingshape.disabled = true
		$crouchshape.disabled = false
		
elif Input.is_action_pressed("right"):
	if Input.is_action_pressed("down"):
		velocity.x = SPEED * 2 
		$AnimatedSprite.play("slide")
		$standingshape.disabled = true
		$crouchshape.disabled = false


#else:
	velocity.x = SPEED - SPEED
	$AnimatedSprite.play("idle")
	$standingshape.disabled = false
	

if is_jumping == true:
	$AnimatedSprite.play("air")

if not.is_on_floor():
	$AnimatedSprite.play("air")
	
if.is_on_floor():
	if Input.is_action_pressed("up"):
		$AnimatedSprite.play("air")
		velocity.y = JUMP_HEIGHT
		
		
if.is_on_wall():
	if Input.is_action_pressed("up"):
		climbing = true;
		$AnimatedSprite.play("climb")
		velocity.y = -75
velocity = move_and_slide(velocity, UP)
:bust_in_silhouette: Reply From: RedBlueCarrots

My best guess is that it’s only playing the first frame because it is constantly switching between the run and slide or jump animations

If the player is pressing right and down, your code will first check if it’s moving right and play the run animations, then checking if it’s sliding and so play the slide animation.

Because godot thinks you are already playing the run animation, it will start the slide animation from the start.

I would recommend combining both times you have an if statement about the horizontal axis. Possibly:

if Input.is_action_pressed("left"):
    velocity.x = -SPEED
    $AnimatedSprite.flip_h = true
    if Input.is_action_pressed("down"):
        velocity.x = -SPEED * 2
        $AnimatedSprite.play("slide")
        $standingshape.disabled = true
        $crouchshape.disabled = false
  else:        
        $AnimatedSprite.play("run")
elif Input.is_action_pressed("right"):
    velocity.x = SPEED
    $AnimatedSprite.flip_h = false
    if Input.is_action_pressed("down"):
        velocity.x = SPEED * 2 
        $AnimatedSprite.play("slide")
        $standingshape.disabled = true
        $crouchshape.disabled = false

  else:
        $AnimatedSprite.play("run")

Let me know if that doesn’t work or you need more clarification

hey bro i tried your code, the slide animation plays all the frames but when i spawn it is automatically playing the run animation and when i slide the animations plays all frames but no matter what i do i cant move

mbarake | 2020-10-03 13:06

This might be my bad, but it’s possible that the indentation is slightly weird? See if this makes a difference:
if Input.is_action_pressed(“left”):
velocity.x = -SPEED
$AnimatedSprite.flip_h = true
if Input.is_action_pressed(“down”):
velocity.x = -SPEED * 2
$AnimatedSprite.play(“slide”)
$standingshape.disabled = true
$crouchshape.disabled = false
else:
$AnimatedSprite.play(“run”)

elif Input.is_action_pressed("right"):
    velocity.x = SPEED
    $AnimatedSprite.flip_h = false
    if Input.is_action_pressed("down"):
        velocity.x = SPEED * 2 
        $AnimatedSprite.play("slide")
        $standingshape.disabled = true
        $crouchshape.disabled = false

    else:
        $AnimatedSprite.play("run")

RedBlueCarrots | 2020-10-04 00:33