How to write a program that if the position of the sprite is equal to these coordinates, the background changed

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

I want to write a script that if my sprite position .x = 1037 and my sprite position.y = 459, Change the background.
At first I think to use Node 2D but because I am new in godot , I think it is better to get a help for this script .
thanks

:bust_in_silhouette: Reply From: Enfyna

If you want to do this with only code you could do :

func _process(delta):
    if $sprite.position.x == 1037 and $sprite.position.y == 459:
        change_background()

But I dont think this is a good approach what I would do is create a new Area2D node. If your sprite parent node is Area2D node then bind the area_entered and area_exited signals. if your sprite parent node is CharacterBody2Dthen bind the body_entered and body_exited signals. After this you can use the signal functions to do what you want.

func _on_area_2d_area_entered(area):
	change_background()
func _on_area_2d_area_exited(area):
	change_background_to_normal()

func _on_area_2d_body_entered(body):
	change_background()
func _on_area_2d_body_exited(body):
	change_background_to_normal()

I think this is a better way to do it. This way you can change the Area2D position and dont have to change the code. More information about Area2D :