1.事件绑定函数:
this.button.on(Laya.Event.CLICK, this, f12);
需要注意的是,第2个参数通常填this,第三个参数则是你绑定的执行函数。
声明执行函数的方式目前我探索到的是两种,第一种是:
你可以声明在函数体内,第二个你声明在类中的函数
2.生命周期,你要尝试绑定函数,那么一定需要有执行入口给你调用,现在你可以通过官网提供的生命周期完成在某个你需要的时间内进行绑定操作。
https://ldc2.layabox.com/doc/?nav=zh-ts-3-4-0
上述连接时官网里面对生命周期的说明。
完整的测试代码:
export default class BtnTest extends Laya.Script{
constructor(){
super();
}
private button : Laya.Button;
onAwake(){
console.info("hello world");
this.button = this.owner.getChildByName("testButton") as Laya.Button;
// console.info(this.owner);
console.info(this.button);
// this.button.clickHandler.method = f12;
//this.button = this.owner as Laya.Button;
this.button.on(Laya.Event.CLICK, this, f12);
function f12() {
console.info("点击测试");
}
}
onTipClick(){
console.info("hello world");
}
}