Animation wont stop after pressing button (animation_finished)

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

So I want my player to attack, and after pressing space (attack button) it won’t stop, I’ve created and connected a signal with this code:

func _on_AnimationPlayer_animation_finished():
  if animationPlayer == "Attack":
	is_attacking = false
	animationPlayer.stop()

the “animationPlayer” is an onready var so i dont have to $AnimationPlayer. I’ve looked for tutorials and similar questions and I have this last signal but it still wont stop

Could you post more code about the logic involving space bar? With only the code posted, it seems you are telling the animation to stop when an animation finishes, but the animation is already finished, so I don’t think animationPlayer.stop() is necessary. Maybe you space bar logic is playing another animation (and so the finished signal is not being emitted)?

godot_dev_ | 2023-01-11 15:18

This is suspicious:

if animationPlayer == "Attack"       

If animationPlayer is an AnimationPlayer node, then this line should be erroring out .
You should be getting Invalid operands 'Object' and 'String' in operator ==.
Since you aren’t getting the error I highly doubt you are getting to that line.
A node type cannot be compared to a literal string. You might be wanting to check a property of that node like ‘name’:

if animationPlayer.name == "Attack"

Because a name property would be a String value.

I would put a breakpoint or print statement on that line to see if you are even getting there.

LeslieS | 2023-01-12 04:22

extends KinematicBody2D

const SPEED = 320
const GRAVITY = 15
const JUMP_POWER = -500
const FLOOR = Vector2(0, -1)

var last_checkpoint: Area2D = null

onready var checkpoint_tween: Tween = get_node(“CheckPointTween”)

var velocity = Vector2()

var on_ground = false
var is_attacking = false

onready var sprite = $Sprite
onready var animationPlayer = $AnimationPlayer

func _physics_process (_delta):

if Input.is_action_pressed("ui_right") && is_attacking == false:
	animationPlayer.play("Run")
	velocity.x = SPEED
	sprite.flip_h = false
elif Input.is_action_pressed("ui_left") && is_attacking == false:
	animationPlayer.play("Run")
	velocity.x = -SPEED
	sprite.flip_h = true
else:
	velocity.x = 0
	if is_attacking == false:
		if on_ground == true:
			animationPlayer.play("Idle")
	
if Input.is_action_pressed("ui_up"):
	if on_ground == true:
		$Jump.play()
		velocity.y = JUMP_POWER
		on_ground = false
	
if Input.is_action_just_pressed("Attack"):
	animationPlayer.play("Attack")
	is_attacking = true
	
velocity.y += GRAVITY

if is_on_floor():
	on_ground = true
	$Jump.stop()
else:
	on_ground = false
	if velocity.y < 0:
		animationPlayer.play("Jump")
	else:
		animationPlayer.play("Fall")
	
	
velocity = move_and_slide (velocity, FLOOR)

func go_to_checkpoint():
checkpoint_tween.interpolate_property(self, “position”, position, last_checkpoint.position, 0, 4, Tween.TRANS_EXPO, Tween.EASE_OUT)
checkpoint_tween.start()

func _on_AnimationPlayer_animation_finished():
if animationPlayer == “Attack”:
is_attacking = false
animationPlayer.stop()

func _on_Bite_area_entered(area):
if area.is_in_group(“hurtbox”):
area.take_damage()

This is currently the whole code, the space key is also set only for the attack in the project settings, but somehow it still doesnt work

GrandeHolt | 2023-01-12 12:27

You were right about the error of string, i changed it but still didnt work

GrandeHolt | 2023-01-12 12:29

:bust_in_silhouette: Reply From: umma

I’m assuming you mean when space key is pressed an attack animation should play. But shouldn’t the animation just stop automatically after it’s finished, if it doesn’t stop, that means you turned on loop in the animation player consider turning that off to see if helps.

You could also try if Input.is_action_just_pressed("space_key") if the above doesn’t work.

I actually had it on loop lol, the good thing is that now it stopped attacking, the bad thing is that i can’t move

GrandeHolt | 2023-01-12 12:31

:bust_in_silhouette: Reply From: GrandeHolt

I found the answer, it was actually simple lol i was dumb, it was
func _on_AnimationPlayer_animation_finished(Attack):
is_attacking = false