Files
godot-docs/classes/class_sprite2d.rst
2025-12-15 22:19:00 +00:00

391 lines
16 KiB
ReStructuredText

:github_url: hide
.. DO NOT EDIT THIS FILE!!!
.. Generated automatically from Godot engine sources.
.. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
.. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Sprite2D.xml.
.. _class_Sprite2D:
Sprite2D
========
**Inherits:** :ref:`Node2D<class_Node2D>` **<** :ref:`CanvasItem<class_CanvasItem>` **<** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
General-purpose sprite node.
.. rst-class:: classref-introduction-group
Description
-----------
A node that displays a 2D texture. The texture displayed can be a region from a larger atlas texture, or a frame from a sprite sheet animation.
.. rst-class:: classref-introduction-group
Tutorials
---------
- `Instancing Demo <https://godotengine.org/asset-library/asset/2716>`__
.. rst-class:: classref-reftable-group
Properties
----------
.. table::
:widths: auto
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`bool<class_bool>` | :ref:`centered<class_Sprite2D_property_centered>` | ``true`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`bool<class_bool>` | :ref:`flip_h<class_Sprite2D_property_flip_h>` | ``false`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`bool<class_bool>` | :ref:`flip_v<class_Sprite2D_property_flip_v>` | ``false`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`int<class_int>` | :ref:`frame<class_Sprite2D_property_frame>` | ``0`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`Vector2i<class_Vector2i>` | :ref:`frame_coords<class_Sprite2D_property_frame_coords>` | ``Vector2i(0, 0)`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`int<class_int>` | :ref:`hframes<class_Sprite2D_property_hframes>` | ``1`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`Vector2<class_Vector2>` | :ref:`offset<class_Sprite2D_property_offset>` | ``Vector2(0, 0)`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`bool<class_bool>` | :ref:`region_enabled<class_Sprite2D_property_region_enabled>` | ``false`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`bool<class_bool>` | :ref:`region_filter_clip_enabled<class_Sprite2D_property_region_filter_clip_enabled>` | ``false`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`Rect2<class_Rect2>` | :ref:`region_rect<class_Sprite2D_property_region_rect>` | ``Rect2(0, 0, 0, 0)`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`Texture2D<class_Texture2D>` | :ref:`texture<class_Sprite2D_property_texture>` | |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
| :ref:`int<class_int>` | :ref:`vframes<class_Sprite2D_property_vframes>` | ``1`` |
+-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
.. rst-class:: classref-reftable-group
Methods
-------
.. table::
:widths: auto
+---------------------------+------------------------------------------------------------------------------------------------------------------+
| :ref:`Rect2<class_Rect2>` | :ref:`get_rect<class_Sprite2D_method_get_rect>`\ (\ ) |const| |
+---------------------------+------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`is_pixel_opaque<class_Sprite2D_method_is_pixel_opaque>`\ (\ pos\: :ref:`Vector2<class_Vector2>`\ ) |const| |
+---------------------------+------------------------------------------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Signals
-------
.. _class_Sprite2D_signal_frame_changed:
.. rst-class:: classref-signal
**frame_changed**\ (\ ) :ref:`🔗<class_Sprite2D_signal_frame_changed>`
Emitted when the :ref:`frame<class_Sprite2D_property_frame>` changes.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_signal_texture_changed:
.. rst-class:: classref-signal
**texture_changed**\ (\ ) :ref:`🔗<class_Sprite2D_signal_texture_changed>`
Emitted when the :ref:`texture<class_Sprite2D_property_texture>` changes.
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Property Descriptions
---------------------
.. _class_Sprite2D_property_centered:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **centered** = ``true`` :ref:`🔗<class_Sprite2D_property_centered>`
.. rst-class:: classref-property-setget
- |void| **set_centered**\ (\ value\: :ref:`bool<class_bool>`\ )
- :ref:`bool<class_bool>` **is_centered**\ (\ )
If ``true``, texture is centered.
\ **Note:** For games with a pixel art aesthetic, textures may appear deformed when centered. This is caused by their position being between pixels. To prevent this, set this property to ``false``, or consider enabling :ref:`ProjectSettings.rendering/2d/snap/snap_2d_vertices_to_pixel<class_ProjectSettings_property_rendering/2d/snap/snap_2d_vertices_to_pixel>` and :ref:`ProjectSettings.rendering/2d/snap/snap_2d_transforms_to_pixel<class_ProjectSettings_property_rendering/2d/snap/snap_2d_transforms_to_pixel>`.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_flip_h:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **flip_h** = ``false`` :ref:`🔗<class_Sprite2D_property_flip_h>`
.. rst-class:: classref-property-setget
- |void| **set_flip_h**\ (\ value\: :ref:`bool<class_bool>`\ )
- :ref:`bool<class_bool>` **is_flipped_h**\ (\ )
If ``true``, texture is flipped horizontally.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_flip_v:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **flip_v** = ``false`` :ref:`🔗<class_Sprite2D_property_flip_v>`
.. rst-class:: classref-property-setget
- |void| **set_flip_v**\ (\ value\: :ref:`bool<class_bool>`\ )
- :ref:`bool<class_bool>` **is_flipped_v**\ (\ )
If ``true``, texture is flipped vertically.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_frame:
.. rst-class:: classref-property
:ref:`int<class_int>` **frame** = ``0`` :ref:`🔗<class_Sprite2D_property_frame>`
.. rst-class:: classref-property-setget
- |void| **set_frame**\ (\ value\: :ref:`int<class_int>`\ )
- :ref:`int<class_int>` **get_frame**\ (\ )
Current frame to display from sprite sheet. :ref:`hframes<class_Sprite2D_property_hframes>` or :ref:`vframes<class_Sprite2D_property_vframes>` must be greater than 1. This property is automatically adjusted when :ref:`hframes<class_Sprite2D_property_hframes>` or :ref:`vframes<class_Sprite2D_property_vframes>` are changed to keep pointing to the same visual frame (same column and row). If that's impossible, this value is reset to ``0``.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_frame_coords:
.. rst-class:: classref-property
:ref:`Vector2i<class_Vector2i>` **frame_coords** = ``Vector2i(0, 0)`` :ref:`🔗<class_Sprite2D_property_frame_coords>`
.. rst-class:: classref-property-setget
- |void| **set_frame_coords**\ (\ value\: :ref:`Vector2i<class_Vector2i>`\ )
- :ref:`Vector2i<class_Vector2i>` **get_frame_coords**\ (\ )
Coordinates of the frame to display from sprite sheet. This is as an alias for the :ref:`frame<class_Sprite2D_property_frame>` property. :ref:`hframes<class_Sprite2D_property_hframes>` or :ref:`vframes<class_Sprite2D_property_vframes>` must be greater than 1.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_hframes:
.. rst-class:: classref-property
:ref:`int<class_int>` **hframes** = ``1`` :ref:`🔗<class_Sprite2D_property_hframes>`
.. rst-class:: classref-property-setget
- |void| **set_hframes**\ (\ value\: :ref:`int<class_int>`\ )
- :ref:`int<class_int>` **get_hframes**\ (\ )
The number of columns in the sprite sheet. When this property is changed, :ref:`frame<class_Sprite2D_property_frame>` is adjusted so that the same visual frame is maintained (same row and column). If that's impossible, :ref:`frame<class_Sprite2D_property_frame>` is reset to ``0``.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_offset:
.. rst-class:: classref-property
:ref:`Vector2<class_Vector2>` **offset** = ``Vector2(0, 0)`` :ref:`🔗<class_Sprite2D_property_offset>`
.. rst-class:: classref-property-setget
- |void| **set_offset**\ (\ value\: :ref:`Vector2<class_Vector2>`\ )
- :ref:`Vector2<class_Vector2>` **get_offset**\ (\ )
The texture's drawing offset.
\ **Note:** When you increase :ref:`offset<class_Sprite2D_property_offset>`.y in Sprite2D, the sprite moves downward on screen (i.e., +Y is down).
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_region_enabled:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **region_enabled** = ``false`` :ref:`🔗<class_Sprite2D_property_region_enabled>`
.. rst-class:: classref-property-setget
- |void| **set_region_enabled**\ (\ value\: :ref:`bool<class_bool>`\ )
- :ref:`bool<class_bool>` **is_region_enabled**\ (\ )
If ``true``, texture is cut from a larger atlas texture. See :ref:`region_rect<class_Sprite2D_property_region_rect>`.
\ **Note:** When using a custom :ref:`Shader<class_Shader>` on a **Sprite2D**, the ``UV`` shader built-in will refer to the entire texture space. Use the ``REGION_RECT`` built-in to get the currently visible region defined in :ref:`region_rect<class_Sprite2D_property_region_rect>` instead. See :doc:`CanvasItem shaders <../tutorials/shaders/shader_reference/canvas_item_shader>` for details.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_region_filter_clip_enabled:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **region_filter_clip_enabled** = ``false`` :ref:`🔗<class_Sprite2D_property_region_filter_clip_enabled>`
.. rst-class:: classref-property-setget
- |void| **set_region_filter_clip_enabled**\ (\ value\: :ref:`bool<class_bool>`\ )
- :ref:`bool<class_bool>` **is_region_filter_clip_enabled**\ (\ )
If ``true``, the area outside of the :ref:`region_rect<class_Sprite2D_property_region_rect>` is clipped to avoid bleeding of the surrounding texture pixels. :ref:`region_enabled<class_Sprite2D_property_region_enabled>` must be ``true``.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_region_rect:
.. rst-class:: classref-property
:ref:`Rect2<class_Rect2>` **region_rect** = ``Rect2(0, 0, 0, 0)`` :ref:`🔗<class_Sprite2D_property_region_rect>`
.. rst-class:: classref-property-setget
- |void| **set_region_rect**\ (\ value\: :ref:`Rect2<class_Rect2>`\ )
- :ref:`Rect2<class_Rect2>` **get_region_rect**\ (\ )
The region of the atlas texture to display. :ref:`region_enabled<class_Sprite2D_property_region_enabled>` must be ``true``.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_texture:
.. rst-class:: classref-property
:ref:`Texture2D<class_Texture2D>` **texture** :ref:`🔗<class_Sprite2D_property_texture>`
.. rst-class:: classref-property-setget
- |void| **set_texture**\ (\ value\: :ref:`Texture2D<class_Texture2D>`\ )
- :ref:`Texture2D<class_Texture2D>` **get_texture**\ (\ )
:ref:`Texture2D<class_Texture2D>` object to draw.
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_property_vframes:
.. rst-class:: classref-property
:ref:`int<class_int>` **vframes** = ``1`` :ref:`🔗<class_Sprite2D_property_vframes>`
.. rst-class:: classref-property-setget
- |void| **set_vframes**\ (\ value\: :ref:`int<class_int>`\ )
- :ref:`int<class_int>` **get_vframes**\ (\ )
The number of rows in the sprite sheet. When this property is changed, :ref:`frame<class_Sprite2D_property_frame>` is adjusted so that the same visual frame is maintained (same row and column). If that's impossible, :ref:`frame<class_Sprite2D_property_frame>` is reset to ``0``.
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Method Descriptions
-------------------
.. _class_Sprite2D_method_get_rect:
.. rst-class:: classref-method
:ref:`Rect2<class_Rect2>` **get_rect**\ (\ ) |const| :ref:`🔗<class_Sprite2D_method_get_rect>`
Returns a :ref:`Rect2<class_Rect2>` representing the Sprite2D's boundary in local coordinates.
\ **Example:** Detect if the Sprite2D was clicked:
.. tabs::
.. code-tab:: gdscript
func _input(event):
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
if get_rect().has_point(to_local(event.position)):
print("A click!")
.. code-tab:: csharp
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseButton inputEventMouse)
{
if (inputEventMouse.Pressed && inputEventMouse.ButtonIndex == MouseButton.Left)
{
if (GetRect().HasPoint(ToLocal(inputEventMouse.Position)))
{
GD.Print("A click!");
}
}
}
}
.. rst-class:: classref-item-separator
----
.. _class_Sprite2D_method_is_pixel_opaque:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **is_pixel_opaque**\ (\ pos\: :ref:`Vector2<class_Vector2>`\ ) |const| :ref:`🔗<class_Sprite2D_method_is_pixel_opaque>`
Returns ``true`` if the pixel at the given position is opaque, ``false`` otherwise. Also returns ``false`` if the given position is out of bounds or this sprite's :ref:`texture<class_Sprite2D_property_texture>` is ``null``. ``pos`` is in local coordinates.
.. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
.. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
.. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
.. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
.. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
.. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
.. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
.. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
.. |void| replace:: :abbr:`void (No return value.)`