0 votes

I am trying to spawn an object "yellowsprite" at the location (0,0) when I press a button "yellowspawn_button". Instead of spawning at (0,0), the object is spawning on top of its parent.

Code:

extends Node2D

var yellow_sprite = preload("yellow_sprite.tscn")
var yellow_sprite_location = Vector2(0,0)

func _on_button_pressed(): "Spawn a yellow pixel when the yellow button is pressed"
    var yellow_sprite_create = yellow_sprite.instance()
    yellow_sprite_location=Vector2(0,0)
    yellow_sprite_create.set_pos(yellow_sprite_location)
    get_node("pixel").add_child(yellow_sprite_create)

func _ready():
    get_node("yellow_spawn_button").connect("pressed",self,"_on_button_pressed")
in Engine by (27 points)
edited by

1 Answer

+1 vote
Best answer

Use yellowspritecreate.set_global_pos(yellowspritelocation) instead of yellowspritecreate.set_pos(yellowspritelocation).

And please use the code button when pasting code here ;-)

by (698 points)
selected by

Thanks for the response!

I have made the suggested replacement, but the yellow pixel still spawns on top of its parent.

Try to add the node to its parent first and then use set_global_pos to set its position.

Perfect, it's working like a charm!

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 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 webmaster@godotengine.org with your username.