From b6bd8399f64272855d5a1d8f6be3be5a44997e8f Mon Sep 17 00:00:00 2001 From: Canvas Date: Sun, 9 Jul 2023 20:05:28 +0800 Subject: [PATCH] fix play animation loop on cache mode. --- cocos/spine/skeleton-cache.ts | 4 ++++ cocos/spine/skeleton.ts | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cocos/spine/skeleton-cache.ts b/cocos/spine/skeleton-cache.ts index 818da6a10e5..0d0377d04c0 100644 --- a/cocos/spine/skeleton-cache.ts +++ b/cocos/spine/skeleton-cache.ts @@ -119,6 +119,10 @@ export class AnimationCache { } } + public getMaxFrameIndex() { + return this._maxFrameIdex; + } + public getFrame (frameIdx: number) { const index = frameIdx % this._maxFrameIdex; return this._frames[index]; diff --git a/cocos/spine/skeleton.ts b/cocos/spine/skeleton.ts index bdf9d163d4f..d650c189a5d 100644 --- a/cocos/spine/skeleton.ts +++ b/cocos/spine/skeleton.ts @@ -718,6 +718,7 @@ export class Skeleton extends UIRenderer { public setAnimation (trackIndex: number, name: string, loop?: boolean): spine.TrackEntry | null { let trackEntry: spine.TrackEntry | null = null; if (loop === undefined) loop = true; + this.loop = loop; if (this.isAnimationCached()) { if (trackIndex !== 0) { warn('Track index can not greater than 0 in cached mode.'); @@ -835,8 +836,10 @@ export class Skeleton extends UIRenderer { this._accTime += dt; const frameIdx = Math.floor(this._accTime / CachedFrameTime); if (this._animCache) { - this._animCache.updateToFrame(frameIdx); - this._curFrame = this._animCache.getFrame(frameIdx); + const isCompleted = this._animCache.updateToFrame(frameIdx); + const maxFrameIdex = this._animCache.getMaxFrameIndex() - 1; + const runFrameIndex = frameIdx >= maxFrameIdex && !this.loop ? maxFrameIdex : frameIdx; + this._curFrame = this._animCache.getFrame(runFrameIndex); } } else { this._instance.updateAnimation(dt);