![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Clockwise_ |
Hello, developers! I was trying to start a project and came across an issue. Basically, I added a TextureRect node to display a 24x24 board (each square has a 64x64 pixels size) and had it centered on the game screen. Then I created an Area2D node for a player (64x64 pixels) and set up a grid-based movement for it to move inside the board. This works fine, but I’d also really like the Area2D node to pop in the top-left square of the board when I run the game. That would be easy if I could get the coordinates of the texture, but they seem not to be available. That’s what I tried (the script is attached to the TextureRect node, and the Area2D node is the root node of the scene Player.tscn):
extends TextureRect
var tile_size = 64
func _ready():
var player = preload("res://Scenes/Player.tscn").instance()
add_child(player)
var viewport_size = get_viewport().get_visible_rect().size
$Area2D.position.x = (viewport_size.x - self.rect_size.y) * 0.5
$Area2D.position.y = (viewport_size.y - self.rect_size.y) * 0.5
I tried to use this code in countless fashions, but it just won’t work. Also, I would like the Area2D node to scale to fit the size of the squares when the screen resolution changes, but I have no clue about that.
Thanks in advance.
Could you please show a picture with the structure of your scene
Wakatta | 2020-11-02 13:15
player.png is the texture of an Area2D node in another scene, which I want to instance here by code.
P.S. In the code snippet, I wrote extends Area2D
but of course I meant extends TextureRect
. I’ve just edited the post. Sorry for the mistake!
Clockwise_ | 2020-11-02 16:53