- Edited
So, I coded this 2D game called "Coin Dash" where a fox has to loot coins and powerups to prevent a timer from going out, leveling up when there is no more loot and increasing coins at each level. There are also two cactus obstacles on the map that are deadly if touched.
For that to work, we should prevent Coins (Area2D Nodes) from spawning inside the obstacles, thus being unreachable. So the coins connect to the signal _on_area_entered:
func _on_area_entered(area):
if area.is_in_group("obstacles"):
position = Vector2(
randi_range(20, screensize.x - 20),
randi_range(20, screensize.y - 20))
As the cactus nodes are in a group called obstacles, they get identified as such, and the coin spawned incorrectly (inside the cactus area) gets repositioned randomly on the map. So apparently this code works sometimes, but eventually the coins are spawned on the obstacles. Let's spawn 1000 coins just to see it happening: