diff --git a/cocos/core/platform/event-manager/event-enum.ts b/cocos/core/platform/event-manager/event-enum.ts index 858b8c1308..47bd2c0ac0 100644 --- a/cocos/core/platform/event-manager/event-enum.ts +++ b/cocos/core/platform/event-manager/event-enum.ts @@ -189,6 +189,13 @@ export enum SystemEventType { * 父节点改变时触发的事件。 */ PARENT_CHANGED = 'parent-changed', + + /** + * @en + * @zh + * 节点销毁时触发的事件。 + */ + NODE_DESTROYED = 'node-destroyed', } ccenum(SystemEventType); diff --git a/cocos/core/scene-graph/base-node.ts b/cocos/core/scene-graph/base-node.ts index 1eae243974..bb48953328 100644 --- a/cocos/core/scene-graph/base-node.ts +++ b/cocos/core/scene-graph/base-node.ts @@ -1404,6 +1404,7 @@ export class BaseNode extends CCObject implements ISchedulable { } } + this.emit(SystemEventType.NODE_DESTROYED, this); return destroyByParent; } diff --git a/cocos/tween/export-api.ts b/cocos/tween/export-api.ts index 351d9aba81..2c8b9b6f21 100644 --- a/cocos/tween/export-api.ts +++ b/cocos/tween/export-api.ts @@ -18,7 +18,7 @@ export type TweenEasing = export interface ITweenOption { easing?: TweenEasing | ((k: number) => number); progress?: (start: number, end: number, current: number, ratio: number) => number; - onStart?: Function; - onUpdate?: Function; - onComplete?: Function; + onStart?: (target?: object) => {}; + onUpdate?: (target?: object, ratio?: number) => {}; + onComplete?: (target?: object) => {}; } \ No newline at end of file diff --git a/cocos/tween/tween.ts b/cocos/tween/tween.ts index 53c33bee9a..d122ade103 100644 --- a/cocos/tween/tween.ts +++ b/cocos/tween/tween.ts @@ -3,7 +3,7 @@ */ import { TweenSystem } from './tween-system'; -import { warn } from '../core'; +import { warn, Node, SystemEventType } from '../core'; import { ActionInterval, sequence, repeat, repeatForever, reverseTime, delayTime, spawn } from './actions/action-interval'; import { removeSelf, show, hide, callFunc } from './actions/action-instant'; import { Action } from './actions/action'; @@ -33,6 +33,9 @@ export class Tween { constructor (target?: object | null) { this._target = target === undefined ? null : target; + if (this._target && this._target instanceof Node) { + this._target.on(SystemEventType.NODE_DESTROYED, this._destroy, this); + } } /** @@ -64,7 +67,15 @@ export class Tween { * @return {Tween} */ target (target: object | null): Tween { + if (this._target && this._target instanceof Node) { + this._target.off(SystemEventType.NODE_DESTROYED, this._destroy, this); + } + this._target = target; + + if (this._target && this._target instanceof Node) { + this._target.on(SystemEventType.NODE_DESTROYED, this._destroy, this); + } return this; } @@ -385,6 +396,10 @@ export class Tween { return action; } + private _destroy () { + this.stop(); + } + private static readonly _tmp_args: Tween[] | Action[] = []; private static _wrappedSequence (...args: Action[] | Tween[]) {