Odd behavior with on_body_entered and on_body_exited

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

I have a KinematicBody2D (player) and an Area2D (ladder). With the following code I get clean enter and exit events that happen as the player touches and leaves the Area2D object e.g. touch Area2D and a single “enter” prints. Move back and a single “exit” prints.

func _on_Ladder_body_entered(body):
	if body.name == "Hero":
		print("enter")
			
func _on_Ladder_body_exited(body):
	if body.name == "Hero":
		print("exit")

with this code things go wrong:

func _on_Ladder_body_entered(body):
	if body.name == "Hero":
		set_collision_layer_bit(0, false)
		print("enter")
			
func _on_Ladder_body_exited(body):
	if body.name == "Hero":
		set_collision_layer_bit(0, true)
		print("exit")

I get an entered event immediately followed by an exit event as soon as the player touches the Area2D. What am I doing wrong? Thanks!

:bust_in_silhouette: Reply From: ponponyaya

I think the question is in the following line:

set_collision_layer_bit(0, false)

It seems like that when the body entered the Area2D, that code line make noone can detecte the Area2D in bit 0. Immediately, the body itself alse can’t detecte the Area2D in bit 0, so the body exited.