Is there a way to get the size of an animated sprite? I see in the docs you can get the texture size of a non-animated sprite but that doesn't seem to be an option for animated sprites.
Get size of an animated sprite?
- Edited
If your sprite is a sprite sheet you should be able to get the node size and divide the x and y dimensions by the tile amounts, no?
It's not a sprite sheet, it's just three individual frames. I want use the size so I can clamp its location and keep it from leaving the screen. I can do this using exact number (which I know), I was just hoping to find something more modular so I didn't have to hard code in the size of my sprite node.
So, why can't you get node size? Are your frames different resolution(and if so, why)?
I don't see an option to get the size of a node, am I missing something? I thought a node was essentially just a point in space and wouldn't have a specific size.
Depends on the type. I meant any kind of size with that. In sprites case I think the following post might answer: https://godotdevelopers.org/forum/discussion/comment/22320/#Comment_22320
Thanks, it seems you can get the size of a texture for a sprite but not an animated sprite since I guess the texture is likely to change sizes per frame. I am able to work around my issue so I'll make this as answered. Thanks for taking the time.
- Edited
I know this is a stale thread, but I needed a solution for this as well and found one.
var new_rect = your_anim_sprite.get_sprite_frames().get_frame("choose_an_animation_name",0).get_size()
You have to drill down through your animation to get to a frame you want to get_size from. Good luck!
A year later, I've found a new solution if anyone needs it. In Godot 4.1.1 in GDScript (I could investigate in C#, C++ if anyone needs it):
# Get the SpriteFrames component of your AnimatedSprite2D node:
var sprite_frames = $AnimatedSprite2D.sprite_frames
# Get the first texture of the wanted animation (in this case, walk, you can also get the size
# in differents cases)
# If your animation frames has different sizes, use $AnimatedSprite2D.frame instead of 0
var texture = sprite_frames.get_frame_texture("walk", 0)
# Get frame size:
var texture_size = texture.get_size()
# This is not the end, you will get the texture size, not the node real size, then you need to
# multiply the texture size with the node scale
var as2d_size = texture_size * $AnimatedSprite2D.get_scale()
Made an account just to thank you for this! Works for me perfectly a year later and was just what I needed.