auto destroy when target is node (#6215)

* add node-destroyed event

* auto destroy when target is node

* tweaks
This commit is contained in:
jiaxin.lai
2020-02-24 14:37:28 +08:00
committed by GitHub
parent 5bad3fbf7c
commit b432674bbb
4 changed files with 27 additions and 4 deletions
@@ -189,6 +189,13 @@ export enum SystemEventType {
* 父节点改变时触发的事件。
*/
PARENT_CHANGED = 'parent-changed',
/**
* @en
* @zh
* 节点销毁时触发的事件。
*/
NODE_DESTROYED = 'node-destroyed',
}
ccenum(SystemEventType);
+1
View File
@@ -1404,6 +1404,7 @@ export class BaseNode extends CCObject implements ISchedulable {
}
}
this.emit(SystemEventType.NODE_DESTROYED, this);
return destroyByParent;
}
+3 -3
View File
@@ -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) => {};
}
+16 -1
View File
@@ -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[]) {