How do I make a sprite hide when it is touched by another sprite?

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

Its a 2d game

I want to hide the seashells if the characters sprite (shark) is touch or reach the seashells

:bust_in_silhouette: Reply From: CollCaz

Add an area2D node as a child of the shark and the shells then add a ColisionShape2D as a child of the Area2D nodes and give it an appropriate shape, then connect the area_shape_entered signal from the area2D of the shark to your player script.

something like this:

func _on_area_2d_area_shape_entered(area_rid, area, area_shape_index, local_shape_index):
hide()

where should i put that code?

Christian Kez | 2023-05-25 03:56

I would add it to the Top most node in your Player scene, so for example if your have the following structure:
Kinematicbody2D [Code attached to this node]

  • Sprite2D
  • Area2D
  • CollisionShape2D

hope that’s clear enough!
however it doesn’t matter where you put this as long as you connect the functions right and you use the hide() function of the parent.

CollCaz | 2023-05-26 13:16

i tried it sir but the shell did not hide when the fish touch it

Christian Kez | 2023-05-28 10:01

fish – Google Drive here is video of the game’s sir, when connecting the nodes, I can’t connect it.

Christian Kez | 2023-05-28 10:02