Skip to content

fixed #17876: [bug][Tween] callFunc before updateUntil may be invoked more than once. #18179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions cocos/tween/actions/action-interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export abstract class ActionInterval extends FiniteTimeAction {
return true;
}

isDone (): boolean {
return (this._elapsed >= this._duration);
override isDone (): boolean {
return this._elapsed >= this._duration && !this.isUnknownDuration();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an action 's duration is unknown, we should not mark it as DONE until it isn't an unknown duration action.

}

_cloneDecoration (action: ActionInterval): void {
Expand All @@ -136,12 +136,16 @@ export abstract class ActionInterval extends FiniteTimeAction {
t = (t < 1 ? t : 1);
this.update(t > 0 ? t : 0);

// NOTE: If the action's duration is unknown, the elapsed time should keep at the point of the last frame,
// NOTE: If the action's duration is unknown, the elapsed time should be kept at the point of the last frame,
// because ActionUnknownDuration will be executed at each frame until its callback returns true.
// After ActionUnknownDuration is finished, the isUnknownDuration method will return false
// and the elapsed time will go as before.
if (this.isUnknownDuration() && !this._firstTick) {
this._elapsed -= dt;
if (t < 1) {
this._elapsed -= dt;
} else {
this._elapsed = this._startTime + this._duration;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If t is 1, that indicates the current ActionInterval action is at the end, so its _elapsed should be start time + duration.

}
}

if (this._firstTick) {
Expand Down
48 changes: 44 additions & 4 deletions tests/tween/tween.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ function runFrames(frames: number) {
}
}

function randomTickSeconds(time: number) {
let totalTime = 0;
for (;;) {
const dt = 0.016 * 2 * Math.random();
director.tick(dt);
totalTime += dt;
if (totalTime >= time) {
break;
}
}
}

test('remove actions by tag', function () {
const scene = new Scene('test-tags');
const node = new Node();
Expand Down Expand Up @@ -4850,7 +4862,7 @@ test('updateUntil 2', function () {
const target2 = { x: 0 };

tween(node)
.delay(1)
.delay(1) // 1s
.sequence(
tween(node).parallel(
tween(node).by(1, { position: v3(90, 90, 90) }).call(cb),
Expand All @@ -4863,10 +4875,10 @@ test('updateUntil 2', function () {
}
return false;
}, 1, false, 'hello'),
tween(node).by(3, { scale: v3(30, 30, 30) })
tween(node).by(3, { scale: v3(30, 30, 30) }) // 3s
),
tween(node).call(cb2),
tween(target2).by(1, { x: 100 }),
tween(target2).by(1, { x: 100 }), // 1s
)
.updateUntil((target: Node, dt: number, arg0: number, arg1: boolean, arg2: string): boolean => {
elapsed += dt;
Expand All @@ -4876,7 +4888,7 @@ test('updateUntil 2', function () {
return true;
}
return false;
}, 2, true, 'world')
}, 2, true, 'world') // 2s
.start();

runFrames(1); // Start
Expand Down Expand Up @@ -4911,6 +4923,34 @@ test('updateUntil 2', function () {
director.unregisterSystem(sys);
});

test('updateUntil 3', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM);

const node = new Node();

let testNumber = 0;
tween(node)
.delay(1)
.call(() => {
testNumber++;
})
.updateUntil(() => {
return false;
})
.start();

runFrames(1); // Start
randomTickSeconds(1);
expect(testNumber).toBe(1);

randomTickSeconds(1);
expect(testNumber).toBe(1);

director.unregisterSystem(sys);
});

test('parallel with two call tween', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
Expand Down
Loading