diff --git a/cocos/tween/tween.ts b/cocos/tween/tween.ts index 3bd5f5c932..aa24c40228 100644 --- a/cocos/tween/tween.ts +++ b/cocos/tween/tween.ts @@ -36,6 +36,7 @@ import { IInternalTweenOption, TweenAction } from './tween-action'; import { SetAction } from './set-action'; import { legacyCC } from '../core/global-exports'; import { Node } from '../scene-graph'; +import type { ActionManager } from './actions/action-manager'; // https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c type FlagExcludedType = { [Key in keyof Base]: Base[Key] extends Type ? never : Key }; @@ -81,6 +82,10 @@ type TweenWithNodeTargetOrUnknown = T extends Node ? Tween : unknown; export type TweenUpdateCallback = (target: T, ratio: number, ...args: Args) => void; export type TweenUpdateUntilCallback = (target: T, dt: number, ...args: Args) => boolean; +function getActionManager (): ActionManager { + return TweenSystem.instance.ActionManager; +} + /** * @en * Tween provide a simple and flexible way to action, It's transplanted from cocos creator。 @@ -309,7 +314,7 @@ export class Tween { return this; } if (this._finalAction) { - TweenSystem.instance.ActionManager.removeAction(this._finalAction); + getActionManager().removeAction(this._finalAction); } const final = this._unionForStart(); this._finalAction = final; @@ -318,7 +323,7 @@ export class Tween { final.setSpeed(this._timeScale); final.setStartTime(time); final.setPaused(false); // If a tween was paused, starting the tween again should clear the 'paused' flag for the final action. - TweenSystem.instance.ActionManager.addAction(final, this._target, false); + getActionManager().addAction(final, this._target, false); } else { warnID(16393); } @@ -336,7 +341,7 @@ export class Tween { if (this._finalAction) { // ActionManager.removeAction will not stop action, so stop it before removeAction. this._finalAction.stop(); - TweenSystem.instance.ActionManager.removeAction(this._finalAction); + getActionManager().removeAction(this._finalAction); this._finalAction = null; } return this; @@ -376,7 +381,7 @@ export class Tween { */ get running (): boolean { if (this._finalAction) { - return TweenSystem.instance.ActionManager.isActionRunning(this._finalAction); + return getActionManager().isActionRunning(this._finalAction); } return false; } @@ -779,7 +784,7 @@ export class Tween { * @zh 目标对象关联的正在运行的缓动实例的个数。 */ static getRunningCount (target: U): number { - return TweenSystem.instance.ActionManager.getNumberOfRunningActionsInTarget(target); + return getActionManager().getNumberOfRunningActionsInTarget(target); } /** @@ -789,7 +794,7 @@ export class Tween { * 停止所有缓动实例 */ static stopAll (): void { - TweenSystem.instance.ActionManager.removeAllActions(); + getActionManager().removeAllActions(); } /** * @en @@ -798,7 +803,7 @@ export class Tween { * 停止指定标签关联的所有缓动实例。 */ static stopAllByTag (tag: number, target?: U): void { - TweenSystem.instance.ActionManager.removeAllActionsByTag(tag, target); + getActionManager().removeAllActionsByTag(tag, target); } /** * @en @@ -807,7 +812,7 @@ export class Tween { * 停止指定对象的关联的所有缓动实例。 */ static stopAllByTarget (target?: U): void { - TweenSystem.instance.ActionManager.removeAllActionsFromTarget(target); + getActionManager().removeAllActionsFromTarget(target); } /** @@ -816,7 +821,7 @@ export class Tween { * @param target @en The target object whose tweens should be paused. @zh 要暂停缓动的目标对象。 */ static pauseAllByTarget (target: U): void { - TweenSystem.instance.ActionManager.pauseTarget(target); + getActionManager().pauseTarget(target); } /** @@ -825,7 +830,7 @@ export class Tween { * @param target @en The target object whose tweens should be resumed. @zh 要恢复缓动的目标对象。 */ static resumeAllByTarget (target: U): void { - TweenSystem.instance.ActionManager.resumeTarget(target); + getActionManager().resumeTarget(target); } private _union (needUpdateOwner: boolean): Sequence | null {