Trying to make so that animated sprite change when entering or exiting an area 2d

Godot Version

4.5

Question

with the current code i came up with,the animations i came up with overlap into each other for some reason instead of one at a time and the right one per area:

If you share code, please wrap it inside three backticks or replace the code in the next block:

extends CharacterBody2D


@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D



var state : String = "(floor)"
var entered_walls : bool = false

func _physics_process(delta: float) -> void:
	if _set_state() == true:		
		update_animation()


func _set_state() -> bool:
	var new_state : String = "(walls)" if entered_walls == true else "(floor)"
	if state == new_state:
		return false
	state = new_state
	return true
	


func update_animation() -> void:
	animated_sprite_2d.play("dancing_souls" + state)
	

func _on_spritechange_area_entered(area: Area2D) -> void:
	entered_walls = true
	


func _on_spritechange_area_exited(area: Area2D) -> void:
	entered_walls = false

The way I debug these issues is by either using the debugger with breakpoints or adding print() statements in strategic locations. Have you tried that.

oh,i used the print but forgot to use the debuger

the print actually detected the area 2d just fine if i remember,will test it again

i think i discovered what is going on,area2d is being entered and exited even when there is no contact with the collision,now i need to figure out what is causing that

1 Like