cocos creator 按钮响应事件
按钮的自定义参数--》是字符串参数,需要转整数
on_button_click:function(e,level){
level = parseInt(level);//按钮传进的参数是字符串
}
按钮响应事件:
onLoad:function(){
this.red_button = this.node.getChildByName("red_button").addComponent(cc.Button);
var click_event = new cc.Component.EventHandler();
click_event.target = this.node;
click_event.component = "组件名";
click_event.handler = "on_red_btn_click";
click_event.customEventdata = "穿的参数";
},
on_red_btn_click:function(e,custom){
}
/ /封装btn监听事件方法
//bt:组件类型,btnName:要绑定的btn名字, handlerName:回调名字,data:穿进去的参数
方法:
bind: function(bt, btnName, handlerName, data) {
var node = new cc.Component.EventHandler();
node.target = this.node;//根节点
node.customEventData = data //参数
node.component = "当前组件的名称";
if (bt.node.name == btnName) {
node.handler = handlerName;
}
bt.clickEvents.push(node);
},
案例:
this.bind(node.getComponent(cc.Button), "kouhong_tab3Item", "tab3ItemBtnCallback", list[i]);
tab3ItemBtnCallback:function(event, customEventData){
console.log("点击btn后,执行的回调方法");
}
//代码触发按钮的响应事件,不用自己去触摸。使用触摸和按键,可以遥控。
this.red_button = this.node.getChildByName("red_button").addComponent(cc.Button);
this.scheduleOnce(function(){
var click_events=this.red_button.clickEvents;
for(var i=0; i<click_events.length; i++) {
var comp_env_handle = click_events[i];
comp_env_handle.emit(["',"ItemBtnCallback"])
}
}.bind(this),3);
ItemBtnCallback:function(event, customEventData){
console.log("点击btn后,执行的回调方法");
}