Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

AnimationPlayer

Inherits: Node < Object

A node used for animation playback.

描述

An animation player is used for general-purpose playback of animations. It contains a dictionary of AnimationLibrary resources and custom blend times between animation transitions.

Some methods and properties use a single key to reference an animation directly. These keys are formatted as the key for the library, followed by a forward slash, then the key for the animation within the library, for example "movement/run". If the library's key is an empty string (known as the default library), the forward slash is omitted, being the same key used by the library.

AnimationPlayer is better-suited than Tween for more complex animations, for example ones with non-trivial timings. It can also be used over Tween if the animation track editor is more convenient than doing it in code.

Updating the target properties of animations occurs at the process frame.

教程

属性

String

assigned_animation

int

audio_max_polyphony

32

String

autoplay

""

String

current_animation

""

float

current_animation_length

float

current_animation_position

AnimationMethodCallMode

method_call_mode

0

bool

movie_quit_on_finish

false

bool

playback_active

float

playback_default_blend_time

0.0

AnimationProcessCallback

playback_process_mode

1

bool

reset_on_save

true

NodePath

root_node

NodePath("..")

float

speed_scale

1.0

Methods

Variant

_post_process_key_value ( Animation animation, int track, Variant value, Object object, int object_idx ) virtual const

Error

add_animation_library ( StringName name, AnimationLibrary library )

void

advance ( float delta )

StringName

animation_get_next ( StringName anim_from ) const

void

animation_set_next ( StringName anim_from, StringName anim_to )

void

clear_caches ( )

void

clear_queue ( )

StringName

find_animation ( Animation animation ) const

StringName

find_animation_library ( Animation animation ) const

Animation

get_animation ( StringName name ) const

AnimationLibrary

get_animation_library ( StringName name ) const

StringName[]

get_animation_library_list ( ) const

PackedStringArray

get_animation_list ( ) const

float

get_blend_time ( StringName anim_from, StringName anim_to ) const

float

get_playing_speed ( ) const

PackedStringArray

get_queue ( )

bool

has_animation ( StringName name ) const

bool

has_animation_library ( StringName name ) const

bool

is_playing ( ) const

void

pause ( )

void

play ( StringName name="", float custom_blend=-1, float custom_speed=1.0, bool from_end=false )

void

play_backwards ( StringName name="", float custom_blend=-1 )

void

queue ( StringName name )

void

remove_animation_library ( StringName name )

void

rename_animation_library ( StringName name, StringName newname )

void

seek ( float seconds, bool update=false )

void

set_blend_time ( StringName anim_from, StringName anim_to, float sec )

void

stop ( bool keep_state=false )


信号

animation_changed ( StringName old_name, StringName new_name )

Emitted when a queued animation plays after the previous animation finished. See queue.

Note: The signal is not emitted when the animation is changed via play or by an AnimationTree.


animation_finished ( StringName anim_name )

Notifies when an animation finished playing.

Note: This signal is not emitted if an animation is looping.


animation_libraries_updated ( )

Notifies when the animation libraries have changed.


animation_list_changed ( )

Notifies when an animation list is changed.


animation_started ( StringName anim_name )

Notifies when an animation starts playing.


caches_cleared ( )

Notifies when the caches have been cleared, either automatically, or manually via clear_caches.


Enumerations

enum AnimationProcessCallback:

AnimationProcessCallback ANIMATION_PROCESS_PHYSICS = 0

Process animation during physics frames (see Node.NOTIFICATION_INTERNAL_PHYSICS_PROCESS). This is especially useful when animating physics bodies.

AnimationProcessCallback ANIMATION_PROCESS_IDLE = 1

Process animation during process frames (see Node.NOTIFICATION_INTERNAL_PROCESS).

AnimationProcessCallback ANIMATION_PROCESS_MANUAL = 2

Do not process animation. Use advance to process the animation manually.


enum AnimationMethodCallMode:

AnimationMethodCallMode ANIMATION_METHOD_CALL_DEFERRED = 0

Batch method calls during the animation process, then do the calls after events are processed. This avoids bugs involving deleting nodes or modifying the AnimationPlayer while playing.

AnimationMethodCallMode ANIMATION_METHOD_CALL_IMMEDIATE = 1

Make method calls immediately when reached in the animation.


Property Descriptions

String assigned_animation

  • void set_assigned_animation ( String value )

  • String get_assigned_animation ( )

If playing, the current animation's key, otherwise, the animation last played. When set, this changes the animation, but will not play it unless already playing. See also current_animation.


int audio_max_polyphony = 32

  • void set_audio_max_polyphony ( int value )

  • int get_audio_max_polyphony ( )

The number of possible simultaneous sounds for each of the assigned AudioStreamPlayers.

For example, if this value is 32 and the animation has two audio tracks, the two AudioStreamPlayers assigned can play simultaneously up to 32 voices each.


String autoplay = ""

  • void set_autoplay ( String value )

  • String get_autoplay ( )

The key of the animation to play when the scene loads.


String current_animation = ""

  • void set_current_animation ( String value )

  • String get_current_animation ( )

The key of the currently playing animation. If no animation is playing, the property's value is an empty string. Changing this value does not restart the animation. See play for more information on playing animations.

Note: While this property appears in the Inspector, it's not meant to be edited, and it's not saved in the scene. This property is mainly used to get the currently playing animation, and internally for animation playback tracks. For more information, see Animation.


float current_animation_length

  • float get_current_animation_length ( )

The length (in seconds) of the currently playing animation.


float current_animation_position

  • float get_current_animation_position ( )

The position (in seconds) of the currently playing animation.


AnimationMethodCallMode method_call_mode = 0

The call mode to use for Call Method tracks.


bool movie_quit_on_finish = false

  • void set_movie_quit_on_finish_enabled ( bool value )

  • bool is_movie_quit_on_finish_enabled ( )

If true and the engine is running in Movie Maker mode (see MovieWriter), exits the engine with SceneTree.quit as soon as an animation is done playing in this AnimationPlayer. A message is printed when the engine quits for this reason.

Note: This obeys the same logic as the animation_finished signal, so it will not quit the engine if the animation is set to be looping.


bool playback_active

  • void set_active ( bool value )

  • bool is_active ( )

If true, updates animations in response to process-related notifications.


float playback_default_blend_time = 0.0

  • void set_default_blend_time ( float value )

  • float get_default_blend_time ( )

The default time in which to blend animations. Ranges from 0 to 4096 with 0.01 precision.


AnimationProcessCallback playback_process_mode = 1

The process notification in which to update animations.


bool reset_on_save = true

  • void set_reset_on_save_enabled ( bool value )

  • bool is_reset_on_save_enabled ( )

This is used by the editor. If set to true, the scene will be saved with the effects of the reset animation (the animation with the key "RESET") applied as if it had been seeked to time 0, with the editor keeping the values that the scene had before saving.

This makes it more convenient to preview and edit animations in the editor, as changes to the scene will not be saved as long as they are set in the reset animation.


NodePath root_node = NodePath("..")

The node from which node path references will travel.


float speed_scale = 1.0

  • void set_speed_scale ( float value )

  • 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.


Method Descriptions

Variant _post_process_key_value ( Animation animation, int track, Variant value, Object object, int object_idx ) virtual const

A virtual function for processing after key getting during playback.


Error add_animation_library ( StringName name, AnimationLibrary library )

Adds library to the animation player, under the key name.


void advance ( float delta )

Shifts position in the animation timeline and immediately updates the animation. delta is the time in seconds to shift. Events between the current frame and delta are handled.


StringName animation_get_next ( StringName anim_from ) const

Returns the key of the animation which is queued to play after the anim_from animation.


void animation_set_next ( StringName anim_from, StringName anim_to )

Triggers the anim_to animation when the anim_from animation completes.


void clear_caches ( )

AnimationPlayer caches animated nodes. It may not notice if a node disappears; clear_caches forces it to update the cache again.


void clear_queue ( )

Clears all queued, unplayed animations.


StringName find_animation ( Animation animation ) const

Returns the key of animation or an empty StringName if not found.


StringName find_animation_library ( Animation animation ) const

Returns the key for the AnimationLibrary that contains animation or an empty StringName if not found.


Animation get_animation ( StringName name ) const

Returns the Animation with the key name. If the animation does not exist, null is returned and an error is logged.


AnimationLibrary get_animation_library ( StringName name ) const

Returns the first AnimationLibrary with key name or null if not found.

To get the AnimationPlayer's global animation library, use get_animation_library("").


StringName[] get_animation_library_list ( ) const

Returns the list of stored library keys.


PackedStringArray get_animation_list ( ) const

Returns the list of stored animation keys.


float get_blend_time ( StringName anim_from, StringName anim_to ) const

Returns the blend time (in seconds) between two animations, referenced by their keys.


float get_playing_speed ( ) 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 play method.

Returns a negative value if the current animation is playing backwards.


PackedStringArray get_queue ( )

Returns a list of the animation keys that are currently queued to play.


bool has_animation ( StringName name ) const

Returns true if the AnimationPlayer stores an Animation with key name.


bool has_animation_library ( StringName name ) const

Returns true if the AnimationPlayer stores an AnimationLibrary with key name.


bool is_playing ( ) const

Returns true if an animation is currently playing (even if speed_scale and/or custom_speed are 0).


void pause ( )

Pauses the currently playing animation. The current_animation_position will be kept and calling play or play_backwards without arguments or with the same animation name as assigned_animation will resume the animation.

See also stop.


void play ( StringName name="", float custom_blend=-1, float custom_speed=1.0, bool from_end=false )

Plays the animation with key name. Custom blend times and speed can be set.

The from_end option only affects when switching to a new animation track, or if the same track but at the start or end. It does not affect resuming playback that was paused in the middle of an animation. If custom_speed is negative and from_end is true, the animation will play backwards (which is equivalent to calling play_backwards).

The AnimationPlayer keeps track of its current or last played animation with assigned_animation. 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.

Note: The animation will be updated the next time the AnimationPlayer is processed. If other variables are updated at the same time this is called, they may be updated too early. To perform the update immediately, call advance(0).


void play_backwards ( StringName name="", float custom_blend=-1 )

Plays the animation with key name in reverse.

This method is a shorthand for play with custom_speed = -1.0 and from_end = true, so see its description for more information.


void queue ( StringName name )

Queues an animation for playback once the current one is done.

Note: If a looped animation is currently playing, the queued animation will never play unless the looped animation is stopped somehow.


void remove_animation_library ( StringName name )

Removes the AnimationLibrary associated with the key name.


void rename_animation_library ( StringName name, StringName newname )

Moves the AnimationLibrary associated with the key name to the key newname.


void seek ( float seconds, bool update=false )

Seeks the animation to the seconds point in time (in seconds). If update is true, the animation updates too, otherwise it updates at process time. Events between the current frame and seconds are skipped.

Note: Seeking to the end of the animation doesn't emit animation_finished. If you want to skip animation and emit the signal, use advance.


void set_blend_time ( StringName anim_from, StringName anim_to, float sec )

Specifies a blend time (in seconds) between two animations, referenced by their keys.


void stop ( bool keep_state=false )

Stops the currently playing animation. The animation position is reset to 0 and the custom_speed is reset to 1.0. See also pause.

If keep_state is true, the animation state is not updated visually.

Note: The method / audio / animation playback tracks will not be processed by this method.