Class AnimatedSprite2D
Sprite node that contains multiple textures as frames to play for animation.
- Inheritance
-
AnimatedSprite2D
Remarks
AnimatedSprite2D is similar to the Sprite2D node, except it carries multiple textures as animation frames. Animations are created using a SpriteFrames resource, which allows you to import image files (or a folder containing said files) to provide the animation frames for the sprite. The SpriteFrames resource can be configured in the editor via the SpriteFrames bottom panel.
See Also
Properties
animation
The current animation from the sprite_frames resource. If this value is changed, the frame counter and the frame_progress are reset.
var animation : StringName = &"default"
Property Value
Remarks
- void set_animation(StringName value)
- StringName get_animation
autoplay
The key of the animation to play when the scene loads.
var autoplay : String = ""
Property Value
Remarks
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 rendering/2d/snap/snap_2d_vertices_to_pixel and rendering/2d/snap/snap_2d_transforms_to_pixel.
var centered : bool = true
Property Value
Remarks
flip_h
If true
, texture is flipped horizontally.
var flip_h : bool = false
Property Value
Remarks
flip_v
If true
, texture is flipped vertically.
var flip_v : bool = false
Property Value
Remarks
frame
The displayed animation frame's index. Setting this property also resets frame_progress. If this is not desired, use AnimatedSprite2D.set_frame_and_progress.
var frame : int = 0
Property Value
Remarks
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
.
var frame_progress : float = 0.0
Property Value
Remarks
offset
The texture's drawing offset.
var offset : Vector2 = Vector2(0, 0)
Property Value
Remarks
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.
var speed_scale : float = 1.0
Property Value
Remarks
sprite_frames
The SpriteFrames resource containing the animation(s). Allows you the option to load, edit, clear, make unique and save the states of the SpriteFrames resource.
var sprite_frames : SpriteFrames
Property Value
Remarks
- void set_sprite_frames(SpriteFrames value)
- SpriteFrames get_sprite_frames
Methods
get_playing_speed
Qualifiers: const
Returns the actual playing speed of current animation or 0
if not playing. This speed is the speed_scale property multiplied by custom_speed
argument specified when calling the AnimatedSprite2D.play method.
Returns a negative value if the current animation is playing backwards.
float get_playing_speed
is_playing
Qualifiers: const
Returns true
if an animation is currently playing (even if speed_scale and/or custom_speed
are 0
).
bool is_playing
pause
Pauses the currently playing animation. The frame and frame_progress will be kept and calling AnimatedSprite2D.play or AnimatedSprite2D.play_backwards without arguments will resume the animation from the current playback position.
See also stop.
void pause
play(StringName, float, bool)
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 AnimatedSprite2D.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.
void play(StringName name, float custom_speed, bool from_end)
Parameters
name
StringNamecustom_speed
floatfrom_end
bool
play_backwards(StringName)
Plays the animation with key name
in reverse.
This method is a shorthand for AnimatedSprite2D.play with custom_speed = -1.0
and from_end = true
, so see its description for more information.
void play_backwards(StringName name)
Parameters
name
StringName
set_frame_and_progress(int, float)
Sets frame the frame_progress to the given values. Unlike setting frame, this method does not reset the frame_progress to 0.0
implicitly.
Example: Change the animation while keeping the same frame and frame_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)
void set_frame_and_progress(int frame, float progress)
Parameters
stop
Stops the currently playing animation. The animation position is reset to 0
and the custom_speed
is reset to 1.0
. See also pause.
void stop
Events
animation_changed
Emitted when animation changes.
signal animation_changed
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.
signal animation_finished
animation_looped
Emitted when the animation loops.
signal animation_looped
frame_changed
Emitted when frame changes.
signal frame_changed
sprite_frames_changed
Emitted when sprite_frames changes.
signal sprite_frames_changed