Files
godot-docs/classes/class_animatedsprite2d.rst
2024-10-04 02:15:35 +00:00

455 lines
20 KiB
ReStructuredText

:github_url: hide
.. DO NOT EDIT THIS FILE!!!
.. Generated automatically from Godot engine sources.
.. Generator: https://github.com/godotengine/godot/tree/4.3/doc/tools/make_rst.py.
.. XML source: https://github.com/godotengine/godot/tree/4.3/doc/classes/AnimatedSprite2D.xml.
.. _class_AnimatedSprite2D:
AnimatedSprite2D
================
**Inherits:** :ref:`Node2D<class_Node2D>` **<** :ref:`CanvasItem<class_CanvasItem>` **<** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
Sprite node that contains multiple textures as frames to play for animation.
.. rst-class:: classref-introduction-group
Description
-----------
**AnimatedSprite2D** is similar to the :ref:`Sprite2D<class_Sprite2D>` node, except it carries multiple textures as animation frames. Animations are created using a :ref:`SpriteFrames<class_SpriteFrames>` resource, which allows you to import image files (or a folder containing said files) to provide the animation frames for the sprite. The :ref:`SpriteFrames<class_SpriteFrames>` resource can be configured in the editor via the SpriteFrames bottom panel.
.. rst-class:: classref-introduction-group
Tutorials
---------
- :doc:`2D Sprite animation <../tutorials/2d/2d_sprite_animation>`
- `2D Dodge The Creeps Demo <https://godotengine.org/asset-library/asset/2712>`__
.. rst-class:: classref-reftable-group
Properties
----------
.. table::
:widths: auto
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`StringName<class_StringName>` | :ref:`animation<class_AnimatedSprite2D_property_animation>` | ``&"default"`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`String<class_String>` | :ref:`autoplay<class_AnimatedSprite2D_property_autoplay>` | ``""`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`bool<class_bool>` | :ref:`centered<class_AnimatedSprite2D_property_centered>` | ``true`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`bool<class_bool>` | :ref:`flip_h<class_AnimatedSprite2D_property_flip_h>` | ``false`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`bool<class_bool>` | :ref:`flip_v<class_AnimatedSprite2D_property_flip_v>` | ``false`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`int<class_int>` | :ref:`frame<class_AnimatedSprite2D_property_frame>` | ``0`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`float<class_float>` | :ref:`frame_progress<class_AnimatedSprite2D_property_frame_progress>` | ``0.0`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`Vector2<class_Vector2>` | :ref:`offset<class_AnimatedSprite2D_property_offset>` | ``Vector2(0, 0)`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`float<class_float>` | :ref:`speed_scale<class_AnimatedSprite2D_property_speed_scale>` | ``1.0`` |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
| :ref:`SpriteFrames<class_SpriteFrames>` | :ref:`sprite_frames<class_AnimatedSprite2D_property_sprite_frames>` | |
+-----------------------------------------+-----------------------------------------------------------------------+-------------------+
.. rst-class:: classref-reftable-group
Methods
-------
.. table::
:widths: auto
+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`float<class_float>` | :ref:`get_playing_speed<class_AnimatedSprite2D_method_get_playing_speed>`\ (\ ) |const| |
+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`is_playing<class_AnimatedSprite2D_method_is_playing>`\ (\ ) |const| |
+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`pause<class_AnimatedSprite2D_method_pause>`\ (\ ) |
+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`play<class_AnimatedSprite2D_method_play>`\ (\ name\: :ref:`StringName<class_StringName>` = &"", custom_speed\: :ref:`float<class_float>` = 1.0, from_end\: :ref:`bool<class_bool>` = false\ ) |
+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`play_backwards<class_AnimatedSprite2D_method_play_backwards>`\ (\ name\: :ref:`StringName<class_StringName>` = &""\ ) |
+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`set_frame_and_progress<class_AnimatedSprite2D_method_set_frame_and_progress>`\ (\ frame\: :ref:`int<class_int>`, progress\: :ref:`float<class_float>`\ ) |
+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`stop<class_AnimatedSprite2D_method_stop>`\ (\ ) |
+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Signals
-------
.. _class_AnimatedSprite2D_signal_animation_changed:
.. rst-class:: classref-signal
**animation_changed**\ (\ ) :ref:`🔗<class_AnimatedSprite2D_signal_animation_changed>`
Emitted when :ref:`animation<class_AnimatedSprite2D_property_animation>` changes.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_signal_animation_finished:
.. rst-class:: classref-signal
**animation_finished**\ (\ ) :ref:`🔗<class_AnimatedSprite2D_signal_animation_finished>`
Emitted when the animation reaches the end, or the start if it is played in reverse. When the animation finishes, it pauses the playback.
\ **Note:** This signal is not emitted if an animation is looping.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_signal_animation_looped:
.. rst-class:: classref-signal
**animation_looped**\ (\ ) :ref:`🔗<class_AnimatedSprite2D_signal_animation_looped>`
Emitted when the animation loops.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_signal_frame_changed:
.. rst-class:: classref-signal
**frame_changed**\ (\ ) :ref:`🔗<class_AnimatedSprite2D_signal_frame_changed>`
Emitted when :ref:`frame<class_AnimatedSprite2D_property_frame>` changes.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_signal_sprite_frames_changed:
.. rst-class:: classref-signal
**sprite_frames_changed**\ (\ ) :ref:`🔗<class_AnimatedSprite2D_signal_sprite_frames_changed>`
Emitted when :ref:`sprite_frames<class_AnimatedSprite2D_property_sprite_frames>` changes.
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Property Descriptions
---------------------
.. _class_AnimatedSprite2D_property_animation:
.. rst-class:: classref-property
:ref:`StringName<class_StringName>` **animation** = ``&"default"`` :ref:`🔗<class_AnimatedSprite2D_property_animation>`
.. rst-class:: classref-property-setget
- |void| **set_animation**\ (\ value\: :ref:`StringName<class_StringName>`\ )
- :ref:`StringName<class_StringName>` **get_animation**\ (\ )
The current animation from the :ref:`sprite_frames<class_AnimatedSprite2D_property_sprite_frames>` resource. If this value is changed, the :ref:`frame<class_AnimatedSprite2D_property_frame>` counter and the :ref:`frame_progress<class_AnimatedSprite2D_property_frame_progress>` are reset.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_property_autoplay:
.. rst-class:: classref-property
:ref:`String<class_String>` **autoplay** = ``""`` :ref:`🔗<class_AnimatedSprite2D_property_autoplay>`
.. rst-class:: classref-property-setget
- |void| **set_autoplay**\ (\ value\: :ref:`String<class_String>`\ )
- :ref:`String<class_String>` **get_autoplay**\ (\ )
The key of the animation to play when the scene loads.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_property_centered:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **centered** = ``true`` :ref:`🔗<class_AnimatedSprite2D_property_centered>`
.. rst-class:: classref-property-setget
- |void| **set_centered**\ (\ value\: :ref:`bool<class_bool>`\ )
- :ref:`bool<class_bool>` **is_centered**\ (\ )
If ``true``, texture will be 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_AnimatedSprite2D_property_flip_h:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **flip_h** = ``false`` :ref:`🔗<class_AnimatedSprite2D_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_AnimatedSprite2D_property_flip_v:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **flip_v** = ``false`` :ref:`🔗<class_AnimatedSprite2D_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_AnimatedSprite2D_property_frame:
.. rst-class:: classref-property
:ref:`int<class_int>` **frame** = ``0`` :ref:`🔗<class_AnimatedSprite2D_property_frame>`
.. rst-class:: classref-property-setget
- |void| **set_frame**\ (\ value\: :ref:`int<class_int>`\ )
- :ref:`int<class_int>` **get_frame**\ (\ )
The displayed animation frame's index. Setting this property also resets :ref:`frame_progress<class_AnimatedSprite2D_property_frame_progress>`. If this is not desired, use :ref:`set_frame_and_progress<class_AnimatedSprite2D_method_set_frame_and_progress>`.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_property_frame_progress:
.. rst-class:: classref-property
:ref:`float<class_float>` **frame_progress** = ``0.0`` :ref:`🔗<class_AnimatedSprite2D_property_frame_progress>`
.. rst-class:: classref-property-setget
- |void| **set_frame_progress**\ (\ value\: :ref:`float<class_float>`\ )
- :ref:`float<class_float>` **get_frame_progress**\ (\ )
The progress value between ``0.0`` and ``1.0`` until the current frame transitions to the next frame. If the animation is playing backwards, the value transitions from ``1.0`` to ``0.0``.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_property_offset:
.. rst-class:: classref-property
:ref:`Vector2<class_Vector2>` **offset** = ``Vector2(0, 0)`` :ref:`🔗<class_AnimatedSprite2D_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.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_property_speed_scale:
.. rst-class:: classref-property
:ref:`float<class_float>` **speed_scale** = ``1.0`` :ref:`🔗<class_AnimatedSprite2D_property_speed_scale>`
.. rst-class:: classref-property-setget
- |void| **set_speed_scale**\ (\ value\: :ref:`float<class_float>`\ )
- :ref:`float<class_float>` **get_speed_scale**\ (\ )
The speed scaling ratio. For example, if this value is ``1``, then the animation plays at normal speed. If it's ``0.5``, then it plays at half speed. If it's ``2``, then it plays at double speed.
If set to a negative value, the animation is played in reverse. If set to ``0``, the animation will not advance.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_property_sprite_frames:
.. rst-class:: classref-property
:ref:`SpriteFrames<class_SpriteFrames>` **sprite_frames** :ref:`🔗<class_AnimatedSprite2D_property_sprite_frames>`
.. rst-class:: classref-property-setget
- |void| **set_sprite_frames**\ (\ value\: :ref:`SpriteFrames<class_SpriteFrames>`\ )
- :ref:`SpriteFrames<class_SpriteFrames>` **get_sprite_frames**\ (\ )
The :ref:`SpriteFrames<class_SpriteFrames>` resource containing the animation(s). Allows you the option to load, edit, clear, make unique and save the states of the :ref:`SpriteFrames<class_SpriteFrames>` resource.
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Method Descriptions
-------------------
.. _class_AnimatedSprite2D_method_get_playing_speed:
.. rst-class:: classref-method
:ref:`float<class_float>` **get_playing_speed**\ (\ ) |const| :ref:`🔗<class_AnimatedSprite2D_method_get_playing_speed>`
Returns the actual playing speed of current animation or ``0`` if not playing. This speed is the :ref:`speed_scale<class_AnimatedSprite2D_property_speed_scale>` property multiplied by ``custom_speed`` argument specified when calling the :ref:`play<class_AnimatedSprite2D_method_play>` method.
Returns a negative value if the current animation is playing backwards.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_method_is_playing:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **is_playing**\ (\ ) |const| :ref:`🔗<class_AnimatedSprite2D_method_is_playing>`
Returns ``true`` if an animation is currently playing (even if :ref:`speed_scale<class_AnimatedSprite2D_property_speed_scale>` and/or ``custom_speed`` are ``0``).
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_method_pause:
.. rst-class:: classref-method
|void| **pause**\ (\ ) :ref:`🔗<class_AnimatedSprite2D_method_pause>`
Pauses the currently playing animation. The :ref:`frame<class_AnimatedSprite2D_property_frame>` and :ref:`frame_progress<class_AnimatedSprite2D_property_frame_progress>` will be kept and calling :ref:`play<class_AnimatedSprite2D_method_play>` or :ref:`play_backwards<class_AnimatedSprite2D_method_play_backwards>` without arguments will resume the animation from the current playback position.
See also :ref:`stop<class_AnimatedSprite2D_method_stop>`.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_method_play:
.. rst-class:: classref-method
|void| **play**\ (\ name\: :ref:`StringName<class_StringName>` = &"", custom_speed\: :ref:`float<class_float>` = 1.0, from_end\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_AnimatedSprite2D_method_play>`
Plays the animation with key ``name``. If ``custom_speed`` is negative and ``from_end`` is ``true``, the animation will play backwards (which is equivalent to calling :ref:`play_backwards<class_AnimatedSprite2D_method_play_backwards>`).
If this method is called with that same animation ``name``, or with no ``name`` parameter, the assigned animation will resume playing if it was paused.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_method_play_backwards:
.. rst-class:: classref-method
|void| **play_backwards**\ (\ name\: :ref:`StringName<class_StringName>` = &""\ ) :ref:`🔗<class_AnimatedSprite2D_method_play_backwards>`
Plays the animation with key ``name`` in reverse.
This method is a shorthand for :ref:`play<class_AnimatedSprite2D_method_play>` with ``custom_speed = -1.0`` and ``from_end = true``, so see its description for more information.
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_method_set_frame_and_progress:
.. rst-class:: classref-method
|void| **set_frame_and_progress**\ (\ frame\: :ref:`int<class_int>`, progress\: :ref:`float<class_float>`\ ) :ref:`🔗<class_AnimatedSprite2D_method_set_frame_and_progress>`
The setter of :ref:`frame<class_AnimatedSprite2D_property_frame>` resets the :ref:`frame_progress<class_AnimatedSprite2D_property_frame_progress>` to ``0.0`` implicitly, but this method avoids that.
This is useful when you want to carry over the current :ref:`frame_progress<class_AnimatedSprite2D_property_frame_progress>` to another :ref:`frame<class_AnimatedSprite2D_property_frame>`.
\ **Example:**\
.. tabs::
.. code-tab:: gdscript
# Change the animation with keeping the frame index and progress.
var current_frame = animated_sprite.get_frame()
var current_progress = animated_sprite.get_frame_progress()
animated_sprite.play("walk_another_skin")
animated_sprite.set_frame_and_progress(current_frame, current_progress)
.. rst-class:: classref-item-separator
----
.. _class_AnimatedSprite2D_method_stop:
.. rst-class:: classref-method
|void| **stop**\ (\ ) :ref:`🔗<class_AnimatedSprite2D_method_stop>`
Stops the currently playing animation. The animation position is reset to ``0`` and the ``custom_speed`` is reset to ``1.0``. See also :ref:`pause<class_AnimatedSprite2D_method_pause>`.
.. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
.. |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.)`