我需要获取Animation总共有几帧,并指定当前播放第几帧。
帧同步项目,已将所有update函数替换成自已的逻辑处理函数,方便丢帧后的补帧。
但是Animation如何在自己的逻辑函数中实现播放,我需要执行一次逻辑函数,Animation播放下一帧。
先计算duration * sample得到总帧数?
然后帧数 / 总帧数得到时间
再animation_comp.setCurrentTime设置播放时间 这样子可以吗
另外有个step方法执行一帧动画倒是没怎么用过 还是setCurrentTime播放到下一帧的时间比较靠谱
2.x的做法
OK,我先试试看
这个是我实现的,对于你的需求来说,似乎明确关注动画状态的的setTime和sample俩个函数就行
public update(entity: ViewState): void {
let state = entity.getState(entity.defaultClip!.name) as AnimationState;
let delta = 0.0;
if (this._direction == 1.0) {
if (state.time >= this._event.frame) {
return;
}
if (entity.trans) {
delta = state.time + game.deltaTime * state.speed;
} else {
delta = this._event.frame;
}
if (delta > this._event.frame) delta = this._event.frame;
} else {
if (state.time <= this._event.frame) {
return;
}
if (entity.trans) {
delta = state.time - game.deltaTime * state.speed;
} else {
delta = this._event.frame;
}
if (delta < this._event.frame) delta = this._event.frame;
}
state.setTime(delta);
state.sample();
}
public static playToFrame(node:Node,clipName:string,toframe:number){
const anim = node.getComponent(Animation);
if(anim){
const ani_state = anim.getState(clipName);
if(ani_state){
//获取每秒帧数(采样)
const fps = ani_state.clip.sample;
//计算要播放的时间
const time = toframe/fps;
//设置当前动画时间
ani_state.setTime(time);
//如果动画当前不是播放状态,可以手动调用update来更新
ani_state.update(toframe);
//anim.play(clipName);
}else{
console.warn("animation clip [ " + clipName + " ] not found.");
}
}else{
console.warn("animation component not found on the node.");
}
}
不知道你说的是不是这个
借楼问一个小问题
clip.events帧事件数据中的func是动画组件所在节点上挂载的脚本中的函数名
在数据序列化反序列化的角度来说是很方便的 但是用代码插入帧事件就没有那么灵活了(实现是实现了 论坛以前也有很多相关讨论)
我希望的是类似于监听播放到第几帧然后传入一个回调的这种形式使用 可能我对监听发射事件理解还没那么透彻 希望有大佬解惑
this.ani[‘playSound’] = (name:string)=>{
//playSound事件帧的回调代码,name是事件帧添加的参数
}
哦哦 就是我上面说的动画组件所在节点上挂载的脚本中的函数 直接定义就好了是吧 
如果是编辑器中插入的事件帧,不需要on,引擎是判断Animation原型上有没有这个key,有的话就把他当函数名来执行,并把编辑器里填的参数传入该函数
可以了 感谢大佬 我第一句话都把答案说出来了但是一下脑筋没转过弯来 
可以的,已经试出来了,感谢!
按照这个思路,已经算出帧数了