0 votes

In 2D, can the child of a node be resized to fit it's parent? In my case, the sprite child to fit the area2d parent

asked Jan 22, 2019 in Engine by YP (38 points)

1 Answer

0 votes

It depends on what the child and the parent are, not all nodes have a size of their own.

For example, you can change the size of a sprite by modifying its scale property.
Then, you need to know the size of your Area2D: this depends on the collision shape you used. If it's a rectangle, you would need to adjust the scale of your sprite in such a way the resulting size is the same as the rectangle. Finally, your sprite needs to be centered on the Area2D.

Probably something like this:

# Assuming the area has a child CollisionShape2D with a RectangleShape resource
var area_size = area.get_node("CollisionShape2D").shape.extents * 2.0

# The size of a sprite is determined from its texture
var texture_size = sprite.texture.get_size()

# Calculate which scale the sprite should have to match the size of the area
var sx = area_size.x / texture_size.x
var sy = area_size.y / texture_size.y

sprite.scale = Vector2(sx, sy)

But this may be wildly different depending on which nodes you have and how they are structured.

answered Jan 23, 2019 by Zylann (26,153 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.