cocos: 模拟点击某个按钮

本文介绍了如何在Cocos Creator中模拟点击按钮的两种方法,适用于游戏开发或移植过程中需要通过代码触发按钮点击事件的场景。参考代码包括使用`clickEvents`属性和另一种替代方法。

参考代码:

 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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值