From 70ee2668c7f3efb4f2a2db3ca34d3e25ded79528 Mon Sep 17 00:00:00 2001 From: Leslie Leigh Date: Tue, 21 May 2019 13:08:43 +0800 Subject: [PATCH] fix animation event bug (#4462) --- cocos/animation/animation-state.ts | 10 ++++++++-- cocos/components/animation-component.ts | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cocos/animation/animation-state.ts b/cocos/animation/animation-state.ts index 2f7b9c0a78..b0476228b8 100644 --- a/cocos/animation/animation-state.ts +++ b/cocos/animation/animation-state.ts @@ -177,6 +177,7 @@ export class AnimationState extends Playable { private _lastWrapInfoEvent: WrappedInfo | null = null; private _process = this.process; private _target: Node | null = null; + private _targetNode: Node | null = null; private _clip: AnimationClip; private _name: string; private _lastIterations?: number; @@ -197,6 +198,7 @@ export class AnimationState extends Playable { public initialize (root: Node) { this._curveLoaded = true; this._curveInstances.length = 0; + this._targetNode = root; const clip = this._clip; this.duration = clip.duration; @@ -290,6 +292,10 @@ export class AnimationState extends Playable { this._target = target; } + public setTargetNode (target: Node) { + this._targetNode = target; + } + public setTime (time: number) { this._currentFramePlayed = false; this.time = time || 0; @@ -618,7 +624,7 @@ export class AnimationState extends Playable { } private _fireEvent (index: number) { - if (!this._target || !this._target.isValid) { + if (!this._targetNode || !this._targetNode.isValid) { return; } @@ -628,7 +634,7 @@ export class AnimationState extends Playable { } const eventGroup = eventGroups[index]; - const components = this._target.components; + const components = this._targetNode.components; for (const event of eventGroup.events) { const { functionName } = event; for (const component of components) { diff --git a/cocos/components/animation-component.ts b/cocos/components/animation-component.ts index f2fdfffc0a..c4aecac6ea 100644 --- a/cocos/components/animation-component.ts +++ b/cocos/components/animation-component.ts @@ -454,7 +454,7 @@ export class AnimationComponent extends Component implements IEventTarget { private _createState (clip: AnimationClip, name?: string) { const state = new AnimationState(clip, name); - state._setEventTarget(this.node); + state._setEventTarget(this); this._nameToState[state.name] = state; return state; }