Search code examples
godotgdscript

Get the pixel size of an AnimatedSprite (gdscript)


I am trying to clamp the player's position so that it's sprite doesn't go over the border, but apart from transform.scale there is no "size" indicator/variable.

position.x = clamp(position.x,0, screen_size.x - scale.x)

Solution

  • What you can do, if all sprites in the animation have the same size :

        var current_animation : String = $AnimatedSprite.animation
        var sprite_texture : Texture = $AnimatedSprite.frames.get_frame(current_animation, 0)
        print(sprite_texture.get_size())
    

    basically what I do here is getting the first texture in the current animation. and read out the size of the texture. This only works, of course, if all frames have the same size.

    ALternativly you should be able to get the current frame by using $AnimatesSprite.frame.