Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(animation): hand off completed native tween loops

yhh19f7ec252797
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 56fc52a41..65869aa19 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Finite native tween loops hand off their sequences.** Restart and ping-pong loops now resume a paused successor when their finite count completes, just like non-looping tweens. Completion leaves cancelled successors cancelled instead of reviving them and writing their target for another frame.
+
- **Looping tweens preserve long-frame remainder.** JS value tweens and native property tweens skip completed cycles arithmetically, retain the current phase and ping-pong direction, and complete finite counts at their final endpoint. Exact cycle boundaries keep their endpoint behavior; delay remainder is preserved and very large frame sums are bounded instead of overflowing elapsed time.
- **Immediate value tweens produce their endpoint.** Non-positive durations complete at the next eligible update, matching native tweens instead of emitting NaN or retaining a negative-duration tween forever. Delays and callback playback controls continue to apply.
diff --git a/sdk/tests/tween-loop-phase-wasm.test.ts b/sdk/tests/tween-loop-phase-wasm.test.ts
index d1a9027da..c56ff5112 100644
--- a/sdk/tests/tween-loop-phase-wasm.test.ts
+++ b/sdk/tests/tween-loop-phase-wasm.test.ts
@@ -14,6 +14,28 @@ describe.skipIf(!HAS_WASM)('loop phase on real WASM', () => {
beforeEach(() => { registry = new engine.Registry(); world = new World(); world.connectCpp(registry, engine); });
afterEach(() => { for (const e of world.getAllEntities()) world.despawn(e); world.disconnectCpp(); registry.delete(); });
+ it.each([LoopMode.None, LoopMode.Restart, LoopMode.PingPong])('resumes native successors after completion: %j', loop => {
+ const api = new TweenAPI(engine, registry), a = world.spawn(), b = world.spawn();
+ world.insert(a, Transform); world.insert(b, Transform);
+ const first = api.to(a, TweenTarget.PositionX, 0, 10, 1, { loop, loopCount: 2 });
+ const next = api.to(b, TweenTarget.PositionX, 0, 20, 10);
+ first.then(next);
+ api.update(2.25);
+ expect(next.state).toBe(TweenState.Running);
+ api.update(.25);
+ expect(world.get(b, Transform).position.x).toBeGreaterThan(0);
+ });
+
+ it('does not revive a native successor cancelled after linking', () => {
+ const api = new TweenAPI(engine, registry), a = world.spawn(), b = world.spawn();
+ world.insert(a, Transform); world.insert(b, Transform);
+ const first = api.to(a, TweenTarget.PositionX, 0, 10, 1);
+ const next = api.to(b, TweenTarget.PositionX, 0, 20, 10);
+ first.then(next); next.cancel();
+ api.update(1);
+ expect(world.get(b, Transform).position.x).toBe(0);
+ });
+
it.each([LoopMode.Restart, LoopMode.PingPong])('matches JS and native loop phase across time steps and counts: %j', loop => {
for (const loopCount of [0, 1, 2, 3, 4]) {
for (const elapsed of [0, .25, 1, 1.25, 2, 2.25, 3, 3.25, 10.25, 1000000.25]) {
diff --git a/src/esengine/animation/TweenSystem.cpp b/src/esengine/animation/TweenSystem.cpp
index a44f90a43..5cf354167 100644
--- a/src/esengine/animation/TweenSystem.cpp
+++ b/src/esengine/animation/TweenSystem.cpp
@@ -84,11 +84,6 @@ void TweenSystem::update(ecs::Registry& registry, f32 deltaTime) {
if (rawT >= 1.0f) {
if (tween.loop_mode == LoopMode::None) {
tween.state = TweenState::Completed;
- if (tween.sequence_next != INVALID_ENTITY) {
- if (auto* next = registry.tryGet<TweenData>(tween.sequence_next)) {
- next->state = TweenState::Running;
- }
- }
pending_remove_.push_back(entity);
} else if (tween.loop_mode == LoopMode::Restart) {
tween.elapsed = 0.0f;
@@ -111,6 +106,12 @@ void TweenSystem::update(ecs::Registry& registry, f32 deltaTime) {
}
}
}
+ if (tween.state == TweenState::Completed && tween.sequence_next != INVALID_ENTITY) {
+ if (auto* next = registry.tryGet<TweenData>(tween.sequence_next);
+ next && next->state == TweenState::Paused) {
+ next->state = TweenState::Running;
+ }
+ }
}
for (auto entity : pending_remove_) {
diff --git a/tests/ecs/test_tween_activity.cpp b/tests/ecs/test_tween_activity.cpp
index ecab151cc..596073feb 100644
--- a/tests/ecs/test_tween_activity.cpp
+++ b/tests/ecs/test_tween_activity.cpp
@@ -8,6 +8,34 @@ using namespace esengine;
using namespace esengine::ecs;
using namespace esengine::animation;
+TEST_CASE("finite looping tween completion resumes its successor") {
+ for (auto mode : {LoopMode::None, LoopMode::Restart, LoopMode::PingPong}) {
+ Registry r; TweenSystem s;
+ auto a = r.create(), b = r.create(); r.emplace<Transform>(a); r.emplace<Transform>(b);
+ auto first = s.createTween(r, a, TweenTarget::PositionX, 0, 10, 1);
+ auto next = s.createTween(r, b, TweenTarget::PositionX, 0, 20, 10);
+ auto& data = r.get<TweenData>(first); data.loop_mode = mode; data.loops_remaining = 2; data.sequence_next = next;
+ r.get<TweenData>(next).state = TweenState::Paused;
+ s.update(r, 2.25f);
+ CHECK_FALSE(r.valid(first));
+ CHECK(r.get<TweenData>(next).state == TweenState::Running);
+ s.update(r, .25f);
+ CHECK(r.get<Transform>(b).position.x > 0);
+ }
+}
+
+TEST_CASE("tween completion does not revive a cancelled successor") {
+ Registry r; TweenSystem s;
+ auto a = r.create(), b = r.create(); r.emplace<Transform>(a); r.emplace<Transform>(b);
+ auto first = s.createTween(r, a, TweenTarget::PositionX, 0, 10, 1);
+ auto next = s.createTween(r, b, TweenTarget::PositionX, 0, 20, 10);
+ r.get<TweenData>(first).sequence_next = next;
+ r.get<TweenData>(next).state = TweenState::Cancelled;
+ s.update(r, 1);
+ CHECK_FALSE(r.valid(next));
+ CHECK(r.get<Transform>(b).position.x == 0);
+}
+
TEST_CASE("looping tweens retain long frame phase") {
for (auto mode : {LoopMode::Restart, LoopMode::PingPong}) {
Registry r; TweenSystem s;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 9685d1879..81c5654b9 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(animation): hand off completed native tween loops':
+ { note: 'Finite native tween loops hand off their sequences.' },
'fix(animation): preserve looping tween phase across long frames':
{ note: 'Looping tweens preserve long-frame remainder.' },
'fix(animation): complete immediate value tweens safely':