0 votes

Hi,

I have this script that plays a flashing animation when an enemy is hit by a bullet:

func _on_Area2D_body_entered(body):
  if isDead: return

  if body.get_name() == "BulletType01":
    body.queue_free()
  if body.get_name() == "PlayerShipRed":
    body.playerHit(1)

  $EnemyHitAudio.play()
  health -= 1
  $Enemy01Sprite.play("hit")

  if health <= 0:
    $Enemy01Sprite.play("explode")
    isDead = true
    emit_signal("stopPath")

As you can see, I'm trying to trigger another animation when the enemy's health reaches 0 or below. However, if the hit animation is playing, it seems as though the death animation is not being played. This is confirmed by the fact that the death animation works every time if I comment out the hit animation.

Any help is appreciated,

Thank you!

asked May 3 in Engine by XavierGerD (18 points)

I cannot reproduce your problem. Can you provide an example project with the issue at hand?

Of course! Here is a link to the github repo:

https://github.com/XavierGerD/Star-Blaster

Just open and run Scene/Main_Game/Main.tscn. Shoot enemies. You should notice death animations are not always playing. The same thing happens if you collide with enemies, your ship should explode but it doesn't always.

Thanks for your help!

1 Answer

0 votes

You have to do this:

func _on_Enemy01Sprite_animation_finished():
    if $Enemy01Sprite.get_animation() == "explode":
        queue_free()

instead of just this:

func _on_Enemy01Sprite_animation_finished():
    queue_free()

or you will free the enemy-node already when the first hit-animation finishes.

answered May 4 by njamster (8,874 points)

Thank you! It works!

Follow up question: The same thing is happening with the player ship. If you collide with enemies, you take damage, eventually your ship should explode. But right now, it seems as though the animation doesn't play if you're already playing one of the tilt animations.

Any clues?

Similar problem. Change this:

func _on_PlayerMovementTween_tween_all_completed():
    $PlayerShipRed.play("idle")

into this:

func _on_PlayerMovementTween_tween_all_completed():
    if not $PlayerShipRed.get_animation() == "explode":
        $PlayerShipRed.play("idle")

Amazing! You are a true wizard! Thank you so much :D

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.