游戏中经常需要将一个spine动画拆成不同帧进行播放
只需要设置几个常用的属性就可以了
animationStart: 开始帧
animationEnd: 结束帧
总帧数可以打印结束帧
timeScale = 0;设置动画停止在自己设置的结束帧上面
this.player.getComponent(sp.Skeleton).setAnimation(0, "nieqiang1", false);
let trackEntry = this.player.getComponent(sp.Skeleton).getCurrent(0)
console.log(trackEntry.animationStart, trackEntry.animationEnd)
trackEntry.animationStart = 0 / 30; // 30是Spine动画的帧率
trackEntry.animationEnd = 25 / 30;
if (DataManager.instance.is_running) {
this.player.getComponent(sp.Skeleton).setAnimation(1, "run2", true);
} else {
this.player.getComponent(sp.Skeleton).setAnimation(1, "1tui2", true);
}
this.player.getComponent(sp.Skeleton).setCompleteListener((trackEntry, loopCount) => {
if (trackEntry.animation.name == 'nieqiang1') {
trackEntry.timeScale = 0;
}
})
一下代码作为参考
// 传入spine与进度比例(注意初始要把spine的TimeScale设置为0)
updateSpine(spine: sp.Skeleton, rate: number) {
const track = spine.getCurrent(0);
// spine.timeScale = 0;
const timeEnd = track.animationEnd;
const current = timeEnd * rate;
track.animationStart = current;
spine.setToSetupPose();
}
/**
* 停止在指定帧。1是开始,-1是最后
* @param frame 帧数
*/
stopAtFrame(frame: number) {
const te = this.player.getComponent(sp.Skeleton).getCurrent(0) as sp.spine.TrackEntry;
// 算出帧对应的时间
let time;
if (frame === -1) {
time = te.animation.duration; // 最后一帧
} else if (frame > 1) {
time = (frame - 1) / 30; // 根据帧率算出对应时间,spine帧率是30
} else {
time = 0; // 首帧
}
// 对time作限制
if (time < 0) time = 0;
if (time >= te.animation.duration) time = te.animation.duration - 0.01; // 太精确的话,动画会停在首帧,所以要减一点
te.timeScale = 0; // 让动画停止
te.trackTime = time;
}