summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2018-05-14 08:30:22 +0200
committerGitHub <noreply@github.com>2018-05-14 08:30:22 +0200
commitaba499965caa78ce1fd9a412ab3f385e1bdf1006 (patch)
tree78efab186ff0b291e71a027d2a20f9f7cf4dc658
parentdd2aba021cade88b6b029071edde634197a253cd (diff)
parentd7f25f7adb12b02c43e60c31bfc3572e4892c3f2 (diff)
Merge pull request #18851 from groud/fix_animatedsprite
Updates frame timeout when changing speed scale
-rw-r--r--scene/2d/animated_sprite.cpp29
-rw-r--r--scene/2d/animated_sprite.h1
2 files changed, 19 insertions, 11 deletions
diff --git a/scene/2d/animated_sprite.cpp b/scene/2d/animated_sprite.cpp
index 60a7961293..54194ff543 100644
--- a/scene/2d/animated_sprite.cpp
+++ b/scene/2d/animated_sprite.cpp
@@ -361,7 +361,7 @@ void AnimatedSprite::_notification(int p_what) {
if (timeout <= 0) {
- timeout = 1.0 / speed;
+ timeout = _get_frame_duration();
int fc = frames->get_frame_count(animation);
if (frame >= fc - 1) {
@@ -483,7 +483,13 @@ int AnimatedSprite::get_frame() const {
void AnimatedSprite::set_speed_scale(float p_speed_scale) {
+ float elapsed = _get_frame_duration() - timeout;
+
speed_scale = MAX(p_speed_scale, 0.0f);
+
+ // We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed
+ _reset_timeout();
+ timeout -= elapsed;
}
float AnimatedSprite::get_speed_scale() const {
@@ -574,21 +580,22 @@ bool AnimatedSprite::is_playing() const {
return playing;
}
-void AnimatedSprite::_reset_timeout() {
-
- if (!playing)
- return;
-
+float AnimatedSprite::_get_frame_duration() {
if (frames.is_valid() && frames->has_animation(animation)) {
float speed = frames->get_animation_speed(animation) * speed_scale;
if (speed > 0) {
- timeout = 1.0 / speed;
- } else {
- timeout = 0;
+ return 1.0 / speed;
}
- } else {
- timeout = 0;
}
+ return 0.0;
+}
+
+void AnimatedSprite::_reset_timeout() {
+
+ if (!playing)
+ return;
+
+ timeout = _get_frame_duration();
}
void AnimatedSprite::set_animation(const StringName &p_animation) {
diff --git a/scene/2d/animated_sprite.h b/scene/2d/animated_sprite.h
index 7b91a1faef..be5b1ef6d6 100644
--- a/scene/2d/animated_sprite.h
+++ b/scene/2d/animated_sprite.h
@@ -141,6 +141,7 @@ class AnimatedSprite : public Node2D {
void _res_changed();
+ float _get_frame_duration();
void _reset_timeout();
void _set_playing(bool p_playing);
bool _is_playing() const;