使用Animation提供的可注册事件--"finshed"时,回调函数中使用cc.AnimationState参数时提示有误

在2.0.7版本中伪代码如下:
protected onLoad() {
try {
this.aniHero.on(“finished”, this.onAnimationFinished, this);
} catch (err) {
logger.error([Hero.onLoad] ${err.stack || err.message});
}
}

  private onAnimationFinished(type: string, state: cc.AnimationState) {
	// 逻辑代码
}

然后在注册事件那段代码this.onAnimationFinished会提示错误:类型“(type: string, state: AnimationState) => void”的参数不能赋给类型“(event: string) => void”的参数。
查看creator.d.ts关于on的定义:on(type: string, callback: (event: Event.EventCustom) => void, target?: any, useCapture?: boolean): (event: Event.EventCustom) => void;
发现callBack中的参数类型是cc.Event.EventCustom,但是我查看api文档发现动画提供的注册事件回调函数的参数是(type: string, state: cc.AnimationState),请问各位大神,如何消除这个报错提示。

1赞

protected onLoad() {
try {
this.aniHero.on(“finished”, this.onAnimationFinished, this);
} catch (err) {
logger.error([Hero.onLoad] ${err.stack || err.message});
}
}

private onAnimationFinished(type: string, state: cc.AnimationState) {
// 逻辑代码
}
代码格式乱了,各位将就看一下

没人吗?哪位引擎大佬出面解一下疑惑?虽然不影响代码运行,但是这个报错看着难受呀。
召唤神龙ing~

还是没人吗?在线等呀等呀

别沉啊

在creator.d.ts添加 on(type: string, callback: (type: string, state: cc.AnimationState) => void, target?: any, useCapture?: boolean): (event: T) => void;解决

2赞

感谢~ 写TS的时候,看问题面板有提示太难受了。
官方目前在v2.3.2版本这个问题依然存在。

如果有遇到同样问题的人,觉得改creator.d.ts文件麻烦的话,也可以这么用:

this.ani.on('finshed',this.aniFinshedhandler,this); // 参数直接什么都不写 aniFinshedhandler(){ //通过这样 也可以获取到 播放结束的动画名称 cc.log(this.ani.currentClip.name); }

1赞

棒!这个帖子我就是在自问自答:joy:

2.3.2,目前用ts编写也会存在相同问题

我在GitHub上也提交了issue:https://github.com/cocos-creator/engine/issues/6615
但是好像没啥反馈 :disappointed: