Make AnimatedSprite able to play backwards
This commit is contained in:
parent
df7d3708c5
commit
4a2c433028
|
@ -24,7 +24,7 @@
|
||||||
<argument index="0" name="anim" type="String" default="""">
|
<argument index="0" name="anim" type="String" default="""">
|
||||||
</argument>
|
</argument>
|
||||||
<description>
|
<description>
|
||||||
Play the animation set in parameter. If no parameter is provided, the current animation is played.
|
Play the animation set in parameter. If no parameter is provided, the current animation is played. Property [code]backwards[/code] plays the animation in reverse if set to [code]true[/code].
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
<method name="stop">
|
<method name="stop">
|
||||||
|
|
|
@ -393,19 +393,30 @@ void AnimatedSprite::_notification(int p_what) {
|
||||||
timeout = _get_frame_duration();
|
timeout = _get_frame_duration();
|
||||||
|
|
||||||
int fc = frames->get_frame_count(animation);
|
int fc = frames->get_frame_count(animation);
|
||||||
if (frame >= fc - 1) {
|
if ((!backwards && frame >= fc - 1) || (backwards && frame <= 0)) {
|
||||||
if (frames->get_animation_loop(animation)) {
|
if (frames->get_animation_loop(animation)) {
|
||||||
frame = 0;
|
if (backwards)
|
||||||
|
frame = fc - 1;
|
||||||
|
else
|
||||||
|
frame = 0;
|
||||||
|
|
||||||
emit_signal(SceneStringNames::get_singleton()->animation_finished);
|
emit_signal(SceneStringNames::get_singleton()->animation_finished);
|
||||||
} else {
|
} else {
|
||||||
frame = fc - 1;
|
if (backwards)
|
||||||
|
frame = 0;
|
||||||
|
else
|
||||||
|
frame = fc - 1;
|
||||||
|
|
||||||
if (!is_over) {
|
if (!is_over) {
|
||||||
is_over = true;
|
is_over = true;
|
||||||
emit_signal(SceneStringNames::get_singleton()->animation_finished);
|
emit_signal(SceneStringNames::get_singleton()->animation_finished);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
frame++;
|
if (backwards)
|
||||||
|
frame--;
|
||||||
|
else
|
||||||
|
frame++;
|
||||||
}
|
}
|
||||||
|
|
||||||
update();
|
update();
|
||||||
|
@ -594,10 +605,12 @@ bool AnimatedSprite::_is_playing() const {
|
||||||
return playing;
|
return playing;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AnimatedSprite::play(const StringName &p_animation) {
|
void AnimatedSprite::play(const StringName &p_animation, const bool p_backwards) {
|
||||||
|
|
||||||
if (p_animation)
|
if (p_animation)
|
||||||
set_animation(p_animation);
|
set_animation(p_animation);
|
||||||
|
|
||||||
|
backwards = p_backwards;
|
||||||
_set_playing(true);
|
_set_playing(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -666,7 +679,7 @@ void AnimatedSprite::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("_set_playing", "playing"), &AnimatedSprite::_set_playing);
|
ClassDB::bind_method(D_METHOD("_set_playing", "playing"), &AnimatedSprite::_set_playing);
|
||||||
ClassDB::bind_method(D_METHOD("_is_playing"), &AnimatedSprite::_is_playing);
|
ClassDB::bind_method(D_METHOD("_is_playing"), &AnimatedSprite::_is_playing);
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("play", "anim"), &AnimatedSprite::play, DEFVAL(StringName()));
|
ClassDB::bind_method(D_METHOD("play", "anim", "backwards"), &AnimatedSprite::play, DEFVAL(StringName()));
|
||||||
ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite::stop);
|
ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite::stop);
|
||||||
ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite::is_playing);
|
ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite::is_playing);
|
||||||
|
|
||||||
|
@ -713,6 +726,7 @@ AnimatedSprite::AnimatedSprite() {
|
||||||
frame = 0;
|
frame = 0;
|
||||||
speed_scale = 1.0f;
|
speed_scale = 1.0f;
|
||||||
playing = false;
|
playing = false;
|
||||||
|
backwards = false;
|
||||||
animation = "default";
|
animation = "default";
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
is_over = false;
|
is_over = false;
|
||||||
|
|
|
@ -128,6 +128,7 @@ class AnimatedSprite : public Node2D {
|
||||||
|
|
||||||
Ref<SpriteFrames> frames;
|
Ref<SpriteFrames> frames;
|
||||||
bool playing;
|
bool playing;
|
||||||
|
bool backwards;
|
||||||
StringName animation;
|
StringName animation;
|
||||||
int frame;
|
int frame;
|
||||||
float speed_scale;
|
float speed_scale;
|
||||||
|
@ -169,7 +170,7 @@ public:
|
||||||
void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
|
void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
|
||||||
Ref<SpriteFrames> get_sprite_frames() const;
|
Ref<SpriteFrames> get_sprite_frames() const;
|
||||||
|
|
||||||
void play(const StringName &p_animation = StringName());
|
void play(const StringName &p_animation = StringName(), const bool p_backwards = false);
|
||||||
void stop();
|
void stop();
|
||||||
bool is_playing() const;
|
bool is_playing() const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue