There's a control flow bug. Right now what's happening is other input events (even things like mouse movement) are triggering your else
control block. That immediately causes the idle animation to play. The function _input(event)
gets called a lot. I think you'll need to add an is_petting
bool to the script, which gets turned to true when the specific action is performed, instead of that action firing a call to $Sprite.play()
.
You can also add a setter to the bool (var is_petting := false setget _on_is_petting_set
) after the variable declaration (), then define the function:
func _on_is_petting_set(value):
is_petting = value
if is_petting:
$Sprite.play("pet")
Note that to get the advantage of the setter I think you have to explicitly say self.is_petting = true
not just is_petting = true
inside your _input(event)
if true
conditional.
(this last part is just based on my best guess of how AnimatedSprite works)
I honestly haven't worked with animations much yet, so if you end up with a Sprite object that performs the pet animation successfully and then stops animating, you might need to explicitly connect the Sprite's animation_finished
signal to a function that calls $Sprite.play('idle')
.