My "Flip" animation is interfering with my "Run" animation. (SOLVED)

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

I followed Heartbeast’s Platform tutorial and went on from there. I’m really happy as it is with some solid controls having only one problem.

My “Flip” animation is interfering with my “Run” animation.

If i remove $Sprite.play("Flip") the “Run” animation works as expected, but the addition of that line of code makes it appear static while the “Flip” animation seems to work fine. I would like to be able to play both animations.

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const SPEED = 240
const JUMP_HEIGHT = -560

var motion = Vector2()

func _physics_process(delta):
motion.y += GRAVITY

if Input.is_action_pressed("ui_right"):
	motion.x = SPEED
	$Sprite.flip_h = false
	$Sprite.play("Flip")
elif Input.is_action_pressed("ui_left"):
	motion.x = -SPEED
	$Sprite.flip_h = true
	$Sprite.play("Flip")

if Input.is_action_pressed("ui_attack"):
		$Sprite.play("Kick")

if is_on_floor():
	if Input.is_action_pressed("ui_right"):
		motion.x = SPEED
		$Sprite.flip_h = false
		$Sprite.play("Run")
	elif Input.is_action_pressed("ui_left"):
		motion.x = -SPEED
		$Sprite.flip_h = true
		$Sprite.play("Run")
	else:
		motion.x = 0
		$Sprite.play("Idle")
	
	if Input.is_action_just_pressed("ui_jump"):
		motion.y = JUMP_HEIGHT
		$Sprite.play("Jump")
	if Input.is_action_pressed("ui_attack"):
		motion.x = 0
		$Sprite.play("Punch")
	if Input.is_action_pressed("ui_down"):
		motion.x = 0
		$Sprite.play("Crouch")

motion = move_and_slide(motion, UP)
pass
:bust_in_silhouette: Reply From: gtkampos

When you use $Sprite.play() (Assuming Sprite is the name of an AnimationPlayer node), it plays from frame 1 to N. In your code, you are playing every step of the _physics_process, SO IT WILL ALWAYS PLAY THE FIRST FRAME OF THE ANIMATION EVERYTIME, making appears to be frozen.

This is the most common solution for animations:

var anim
var next_anim

func _physics_process(delta):
    if Input.is_action_pressed("ui_right"):
        motion.x = SPEED
        $Sprite.flip_h = false
        next_anim = "Run"
    elif Input.is_action_pressed("ui_left"):
        motion.x = -SPEED
        $Sprite.flip_h = true
        next_anim = "Run"

    if anim != next_anim:
        anim = next_anim
        $Sprite.play(anim)

Another problem is why you are using two input checks. You only have ONE input check of every key, and respond to it. So, you have to design your code better.

I don’t have an AnimationPlayer node setup, it wasn’t used in the tutorial.

Ok so i made some changes to the code, i got exactly the same results, wich is interesting but still no run animation.

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const SPEED = 240
const JUMP_HEIGHT = -560

var motion = Vector2()

func _physics_process(delta):
motion.y += GRAVITY

if Input.is_action_pressed("ui_right"):
	motion.x = SPEED
	$Sprite.flip_h = false
	$Sprite.play("Flip")
	if is_on_floor():
		$Sprite.play("Run")
elif Input.is_action_pressed("ui_left"):
	motion.x = -SPEED
	$Sprite.flip_h = true
	$Sprite.play("Flip")
	if is_on_floor():
		$Sprite.play("Run")
else:
	motion.x = 0
	if is_on_floor():
		$Sprite.play("Idle")

if Input.is_action_pressed("ui_attack"):
		$Sprite.play("Kick")

if is_on_floor():
	if Input.is_action_pressed("ui_attack"):
		motion.x = 0
		$Sprite.play("Punch")
	if Input.is_action_pressed("ui_down"):
		motion.x = 0
		$Sprite.play("Crouch")
	if Input.is_action_just_pressed("ui_jump"):
		motion.y = JUMP_HEIGHT
		$Sprite.play("Jump")

motion = move_and_slide(motion, UP)
pass

KND2501 | 2018-02-23 16:27

:bust_in_silhouette: Reply From: KND2501

I’ve done it! I don’t know why this works, but it works! Both my “Flip” and “Run” animation play as they should be doing. Problem solved.

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const SPEED = 240
const JUMP_HEIGHT = -560

var motion = Vector2()

func _physics_process(delta):
motion.y += GRAVITY

if Input.is_action_pressed("ui_right"):
	motion.x = SPEED
	$Sprite.flip_h = false
	if is_on_floor():
		$Sprite.play("Run")
	else:
		$Sprite.play("Flip")
elif Input.is_action_pressed("ui_left"):
	motion.x = -SPEED
	$Sprite.flip_h = true
	if is_on_floor():
		$Sprite.play("Run")
	else:
		$Sprite.play("Flip")
else:
	motion.x = 0
	if is_on_floor():
		$Sprite.play("Idle")

if Input.is_action_pressed("ui_attack"):
		$Sprite.play("Kick")

if is_on_floor():
	if Input.is_action_pressed("ui_attack"):
		motion.x = 0
		$Sprite.play("Punch")
	if Input.is_action_pressed("ui_down"):
		motion.x = 0
		$Sprite.play("Crouch")
	if Input.is_action_just_pressed("ui_jump"):
		motion.y = JUMP_HEIGHT
		$Sprite.play("Jump")

motion = move_and_slide(motion, UP)
pass