How to configure a deadly object

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

Hi guys I know how to restart the game but I do not know how can I configure an object that triggers the restart. For example in an endless runner like flappy bird are the tubes deadly for the player. How can I make the tubes deadly?

Let me see if I understand this correctly. You need a state such that, when the main character is defeated (e.g. “killed”), it then resets the game? If that’s correct, your game needs a “game over” trigger (the bird touching the tubes, for instance). In the case of Flappy Birds, the “game over” trigger looks like this:

  1. Move bird according to user’s input.
  2. If the bird collides with a pipe (one that may have an Area2D node), go to the “game over” state.
  3. Reset the game from the beginning.

Ertain | 2019-12-30 00:18

And how can I collide the bird with the object?

AU_38 | 2019-12-30 02:38

:bust_in_silhouette: Reply From: johnygames

Assuming you have a 2D flappy bird-type game, you need to place Area2D nodes on both the bird and the tube.

  1. Create Area2D nodes for your bird and tube objects

  2. Create a new script for the tube.

  3. Set up an area_entered signal from the Node tab (next to the Inspector tab when you select an object in the editor). Connect the signal to the node that holds the script.

  4. A new _on_Area2D_area_entered(area) function appears in the previously created script. Put your logic here. The gist of it all is that when another Area2D collides with this object, you want to check what this other Area2D is, and if it is the bird, you restart the game. You do it somewhat like this:

func _on_Area2D_area_entered(area):
	if area.name == "Area2D":
		get_tree().change_scene(MainScreen) # change to another scene