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

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By YP

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

:bust_in_silhouette: Reply From: Zylann

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.