欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

cocosCreator 发射事件

程序员文章站 2022-06-11 10:05:33
...
cc.Class({
    extends: cc.Component,

    properties: {

    },

    start () {
        this.button = this.node.getComponent(cc.Button);
       // this.node.on('click',this.clickButton,this);
      
    },

    clickButton:function(button)
    {
        cc.log('click is ok');
       // cc.director.loadScene('GameMainScene');//切换场景
        this.node.emit('receiveEvent',"发送事件消息");//同个节点的脚本才能收到
        //this.node.dispatchEvent('receiveEvent','发送事件消息');//父节点才能收到
    },
    
    loadScene:function()
    {
        cc.log('loadedScene is finish');
       var spriteNode = cc.find('sprite');
       cc.log('spriteNode = '+spriteNode);
       var loadReCallBack = function(err,sFrame)
       {
        spriteNode.getComponent(cc.Sprite).spriteFrame = sFrame;
       }
       cc.loader.loadRes("head_1",loadReCallBack);
    }
});

//当收到发送的MSG时调用以下函数;

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.node.on('receiveEvent',this.receiveEvent,this);
    },

    // update (dt) {},

    receiveEvent:function(event)
    {
        cc.log('receiveEvent')
    }
});

 

相关标签: cocosCreator