collision detection not working with RectangleShape2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PrOoOg
:warning: Old Version Published before Godot 3 was released.
extends Sprite

var collision_box_ennemy
var collision_box_player
var player_sprite

func _ready():
	collision_box_ennemy = RectangleShape2D.new()
	collision_box_player = RectangleShape2D.new()
	player_sprite = get_node("/root/level/player/player_sprite")
	set_process(true)
	
func _process(delta):
	collision_box_ennemy.set_extents(Vector2(self.get_texture().get_size().width/2, self.get_texture().get_size().height/2))
	collision_box_player.set_extents(Vector2(player_sprite.get_texture().get_size().width/2, player_sprite.get_texture().get_size().height/2))
	
	if collision_box_ennemy.collide(get_transform(), collision_box_player, player_sprite.get_transform()):
		print("collision")

this is not working although i don’t have any errors.

:bust_in_silhouette: Reply From: YeOldeDM

The collision shapes need to be added as children to the physics bodies they belong to. Otherwise, they are not added to the scenetree, and will not “exist” in your gamespace.