cocos-creator中几种事件

cc.Class({
    extends: cc.Component,

    properties: {

    },

     onLoad:function () {
//1.系统按键监听事件
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
//2.用来传递参数
        this.node.on("foot",function(arg1,arg2,arg3)
        {
            cc.log(arg1,"  ",arg2,"  ",arg3);
            cc.log("调用了函数");
    
        });
     },

    start () {
//3.按键监听事件
    this.node.on(cc.Node.EventType.TOUCH_START,this.onMove,this);

    },

    update (dt) {
 
    },

    onKeyDown:function(event)
    {
        switch(event.keyCode){
            case cc.macro.KEY.a:
            cc.log("666");
            break;
        }
    },

    onMove:function(touch)
    {
        var sp=cc.find('iamg');
        sp.position=touch.getLocation();
    }
    
//用emit给on传递参数
       this.node.emit("foot",1,2,3);
});

//4.子节点给父节点传递消息,只可以子传父节点

//子节点写
 this.node.dispatchEvent(new cc.Event.EventCustom("dis",true));

//父节点写

    this.node.on('dis',function(event)
    {
        cc.log("获得了消息")
    })

//5.全局传递事件

//在本类中
   cc.systemEvent.on("666",function()
        {
            cc.log("666");
        })

//在其他类中
  cc.systemEvent.emit("666");

猜你喜欢

转载自blog.csdn.net/piyixia/article/details/88909461