How to add a 'death' animation for Dodge the Creeps tutorial? [SOLVED]

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

Hi, nice to meet you guys, Im John (44yo) I have been learning about Godot, I love it. I have been reading the documentation; I think that what I want (add a ‘death’ animation for Dodge the Creeps tutorial) will be forward after I have understanding of the GDScript language, I already made the game according to the tutorial and added some details on my own, but I can’t continue with the next docs until I manage to add that animation (personal issue), because I don’t want the player to simply disappear when it is hit. If I move on I will keep thinking on that death animation and dont concentrate in tne next topics, just need that to move on.

The line I have is $Sprite_player.animation = “dead”, player have his own custom sprite for that but is the code that I dont know how or where to put it. Does it go in the player script? below 'func _on_Player_body_entered(_body):? instead of hide() ofcourse… doesnt work, need create a var? or create a func first?

Please help, I realy want to continue the learning and move on with the next documentations, but Im stuck with this for days

1 Like
:bust_in_silhouette: Reply From: Ertain

The animation should be in the _on_Player_body_entered() function. It should be played before hide(). But the collision shape for the player should be disabled while the animation is playing, so that there isn’t any other things happening to the player (which could look messy). The code could look like the following (I haven’t tested it, BTW):

func _on_Player_body_entered(_body):
    $CollisionShape2D.set_deferred("disabled", true)
    emit_signal("hit")
    $Sprite_player.play("death")
    yield($Sprite_player, "animation_finished")
    hide()

Thanks for your answer

Prokuneo | 2021-11-06 19:29

I had thrown that together quickly. The $Sprite_player.play("death") part should be changed to whatever animates the “death” animation on the player. Also, I don’t know the code that’s triggered with the “hit” signal.

Ertain | 2021-11-06 20:22

I’ve tryed with

$Sprite_player.animation = “death” (original line for movements, right, left, etc, animation works fine if I sustitute the “walk” or the “up” anim.)
and a large list of possibles combinations calling that “death” animation, but doesnt work in your code :frowning:

and now… yours
$Sprite_player.play(“death”)
yield($Sprite_player, “animation_finished”) doesnt work either, with the news that player sprite at least does not disappear immediately.

About the “hit” signal, the only thing I know is that triggered itself when
body_entered (mobs collitions) colide with player, just did like the tutorial says.

Prokuneo | 2021-11-06 21:23

:bust_in_silhouette: Reply From: Prokuneo

Thanks @Ertain

What happens with the code that you have given me very kindly, and the logical and understandable explanation is that the player continues to show its original movement animation (it does not call the death animation even when it clearly says $Sprite_player.play (“death”)), instead if I do not move the character, it remains there on the screen without disappearing (appear too play button, game title, etc), when moving it towards any direction it makes its characteristic walking animation and then disappears (hide ()), not the death anim.

The player already consists of all 3 sprites (death, up and walk)

Any idea?

:bust_in_silhouette: Reply From: Prokuneo

Sorry to bother, but does anyone have any idea what might be going on? why with the command that was given me the death animation does not appear but the movement one by default? it’s wrong?
Is that the correct way to call the additional death animation? What others could they be for me to try?

:bust_in_silhouette: Reply From: Prokuneo

I GOT IT! I leave it here in case someone else wants to go a little beyond the tutorial

1- Firts, create a variable at startup and declare it false

var is_dead = false

2.- Declare it at the beginning of func _process(delta): as a false condition, so that it is alive and can move (and move the rest of the _process → to be inside of “if is_dead == false”

func _process(delta):
… if is dead == false:
… var velocity = Vector2()
… if Input.is_action_pressed(“ui_right”):
… velocity.x += 1
etc etc… etc…

3.- Add this lines at func _on_Player_body_entered(_body): that specifies what to do when the players dies:

func _on_Player_body_entered(_body):
… var velocity = Vector2() #to call states of movements #NEW
… is_dead = true #to declare that… is not live… duh #NEW
… velocity = Vector2(0, 0) #to make him stop #NEW

… $CollisionShape2D.set_deferred(“disabled”, true)
… emit_signal(“hit”)
… $AnimatedSprite.play(“death”) #to play your animated sprite for death for the player #NEW
… yield($AnimatedSprite, “animation_finished”) #to hold the next “hide” until the death animation is finished #NEW

… hide()

4.- Make him life again at the beginning of the function start:

func start(pos):

… is_dead = false #New
… position = pos
… show()
… $CollisionShape2D.disabled = false

Glad that you solved it.

Ertain | 2021-11-09 21:55

:bust_in_silhouette: Reply From: Prokuneo

here is the visual result