0 votes

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 .

Godot version 4
in Engine by (18 points)

1 Answer

0 votes

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:

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):
func _on_area_2d_area_exited(area):

func _on_area_2d_body_entered(body):
func _on_area_2d_body_exited(body):

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 :

by (464 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.