参考代码:
4/21/2020 11:59:12 PM
var doublenode = cc.find("**/doublenode_name");
doublenode.dispatchEvent(new event("click"));
var screen_width = 640;
var width_active = 156;
var width_normal = (screen_width - 156) / 4;
var SpriteNameNormal = 'sprite_normal';
var SpriteNameActive = 'sprite_active';
cc.Class({
extends: cc.Component,
properties: {
curActive: {
default: 0,
type: cc.Integer,
},
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.btns = [];
for (i = 0; i < 5;++i) {
var btn = this.node.getChildByName('button' + (i + 1));
btn.getChildByName(SpriteNameActive).width = width_active;
/*
btn.on(cc.Node.EventType.TOUCH_END, function(t){
// console.log("cc.Node.EventType.TOUCH_END ");
// console.log(t);
var name = t.target._name;
for (i = 0;i < 5;i++) {
if (name == this.btns[i].name) {
this.curActive = i;
this._onButtonClick();
break;
}
}
},this);
*/
this.btns.push(btn);
}
},
start () {
this.curActive = 2;
this._onButtonClick();
},
// update (dt) {},
onButtonClick: function(evt) {
var bname = evt.target._name;
for (i = 0;i < 5;i++) {
if (bname == this.btns[i].name) {
this.curActive = i;
this._onButtonClick();
break;
}
}
},
_onButtonClick: function() {
for (i = 0; i < 5;++i) {
var btn = this.btns[i];
if (i == this.curActive) {
btn.getChildByName(SpriteNameNormal).active = false;
btn.getChildByName(SpriteNameActive).active = true;
btn.width = width_active;
} else {
btn.getChildByName(SpriteNameNormal).active = true;
btn.getChildByName(SpriteNameActive).active = false;
btn.width = width_normal;
}
}
},
});
note:
//alert("activeQZMenu: e = "+e);
this.robMultipleBtBox.active = e;
//alert("this.bt_2:"+this.robMultipleBtBox.getChildByName("bt_2"));
//alert("this.bt_2:"+this.bt_2);
//this.robMultipleBtBox.children.bt_2.click();
if(e === true)
this.robMultipleBtBox.getChildByName("bt_2").click();
//this.robMultipleBtBox.getChildByName("bt_2").clickEvents[0].emit(['click']);
//this.robMultipleBtBox.getChildByName("bt_2").dispatchEvent(new event("click"));
alert("done ? this.bt_2:"+this.robMultipleBtBox.getChildByName("bt_2"));
//this.robMultipleBtBox.active = false
最后测试终于可以了:
this.node.getComponent(cc.Button).clickEvents[0].emit(['click']);
参考这里:
var fk = this.robMultipleBtBox.getChildByName("bt_2").getComponent(cc.Button);
alert("fk"+fk);
//document.createEvent("MouseEvents");
//e.initEvent("click", true, true);
//this.robMultipleBtBox.getChildByName("bt_2").dispatchEvent(e);
//this.robMultipleBtBox.getChildByName("bt_2").clickEvents[0].emit(['click']);//not work
//this.robMultipleBtBox.getChildByName("bt_2").dispatchEvent(new event("click"));
//this.robMultipleBtBox.getChildByName("bt_3").dispatchEvent(cc.ClickEvent);
this.robMultipleBtBox.getChildByName("bt_2").getComponent(cc.Button).clickEvents[0].emit(['click']);
//this.node.getComponent(cc.Button).clickEvents[0].emit(['click']);
alert("after ? this.bt_2:"+this.robMultipleBtBox.getChildByName("bt_2"));
//this.robMultipleBtBox.getChildByName("bt_2").click();//not work
https://forum.cocos.org/t/topic/40282/3
this.node.getComponent(cc.Button).clickEvents[0].emit([‘click’]);
这个方法亲测可行,参考:http://www.john3.cn/2018/11/12/diary/d20181112/
原文保存下:
添加链接描述
creator 模拟按钮点击事件
做移植过程中,很多时候需要模拟按钮点击事件,比如:通过手柄的a,模拟点击某个按钮。
代码如下:
this.node.getComponent(cc.Button).clickEvents[0].emit(['click']);
顺便记录一下为按钮添加事件的两种方法
方法一:
var clickEventHandler = new cc.Component.EventHandler();
clickEventHandler.target = this.node; //JS绑定的 node 节点
clickEventHandler.component = "MyComponent";//JS文件名
clickEventHandler.handler = "callback";
clickEventHandler.customEventData = "xxx";
var button = this.node.getComponent(cc.Button);
button.clickEvents.push(clickEventHandler);
callback (event, customEventData) {
//event.target 为按钮绑定的node
//customEventData 为上面的 "xxx"
}
方法二:
this.node.on('click', this.callback, this);
callback (event) {
//event.detail 为cc.Button
}
本文介绍了如何在Cocos Creator中模拟点击按钮的两种方法,适用于游戏开发或移植过程中需要通过代码触发按钮点击事件的场景。参考代码包括使用`clickEvents`属性和另一种替代方法。
860

被折叠的 条评论
为什么被折叠?



