0 votes

I have a Node2D with a script. The node2d is located at (0, 200). I have this script attached.

extends Node2D

func init():

func _ready():
    var tile_scene = preload("res://scenes/tile.tscn")
    for i in range(9):
        var tile = tile_scene.instance()
        tile.set_position(Vector2(i * 50, 0))
        get_parent().call_deferred("add_child", tile)

#func _process(delta):
#   # Called every frame. Delta is time since last frame.
#   # Update game logic here.
#   pass

Now when I run this, the 9 objects i create start at 0,0, and increment in the X axis by 50 each time. But they are all situated on y = 0, but I thought since they were children of a Node2D sitting at (0, 200), they would all start at (0, 200) and move in the X direction.

What am I doing wrong? Thanks.

in Engine by (12 points)

2 Answers

0 votes

Reference the node's y at the y of your set_position with position.y.

I think it's not setting the position as a would-be child because it's just basing it off the Node2D's parent e.g. root based on where the code is.

by (150 points)
0 votes

"I thought since they were children of a Node2D sitting at (0, 200)..."

I don't think they are:

get_parent().call_deferred("add_child", tile)

This adds the tile as a child of the Node2D's parent, not as a child of the Node2D itself. If you change it to just call_deferred("add_child", tile) does that behave as you expect?

by (624 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 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.