Skip to content

Commit

Permalink
fix play animation loop on cache mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Canvasfull committed Jul 9, 2023
1 parent 5ba42f2 commit b6bd839
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cocos/spine/skeleton-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
7 changes: 5 additions & 2 deletions cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b6bd839

Please sign in to comment.