Cocos Creator 系统学习笔记(三)--事件响应

事件响应:

 
cc.Class({
    extends: cc.Component,

    properties: {
       
    }, 
/**触摸事件
 * 1: 触摸事件类型: START, MOVED, ENDED(物体内), CANCEL(物体外);
2: 监听触摸事件: node.on(类型, callback, target(回掉函数的this), [useCapture]);
3: 关闭触摸事件: node.off(类型, callback, target(回掉函数的this), [useCapture]);
4: targetOff (target): 移除所有的注册事件;
5: 回掉函数的参数设置  function(t(cc.Touch))
6: cc.Touch: getLocation返回触摸的位置;getDelta返回距离上次的偏移;
7: cc.Event: stopPropagationImmediate/stopPropagation 停止事件的传递;
8: 事件冒泡: 触摸事件支持节点树的事件冒泡,会从当前前天往上一层一层的向父节点传送;
9: 可以使用stopPropagationImmediate停止事件传递。
 * 
 */
    onLoad0:function () {
        // (1) 监听对应的触摸事件: 向引擎底层注册一个回掉函数,当有触摸事件发生的时候调用该回掉函数;
        // cc.Node.EventType.TOUCH_START: 触摸开始
        // cc.Node.EventType.TOUCH_MOVE: 触摸移动
        // cc.Node.EventType.TOUCH_END: 触摸结束, (物体内部结束)
        // cc.Node.EventType.TOUCH_CANCEL: 触摸结束, (物体外部结束)
        /*
        (2) 触摸函数的回掉函数格式  function(t)  -->t:对象。是 cc.Touch对象触摸事件对象 {触摸信息,事件信息}
        call --> this, this指向谁就是这个target(可选参数);你要绑那个对象作为你回掉函数的this, 可以为空
        或者显示绑定function () {}.bind(this); 
        this.node.on(tyep,callback,target);
        */
       this.node.on(cc.Node.EventType.TOUCH_START, function(t) {
        console.log("cc.Node.EventType.TOUCH_START called"); 
        // 停止事件传递
        //t.stopPropagationImmediate(); 
    }, this);

    this.node.on(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move, this);
    

    this.node.on(cc.Node.EventType.TOUCH_END, function(t) {
        console.log("cc.Node.EventType.TOUCH_END called");
    }.bind(this));

    this.node.on(cc.Node.EventType.TOUCH_CANCEL, function(t) {
        console.log("cc.Node.EventType.TOUCH_CANCEL called");
    }, this);

    // 移除
    // this.node.off(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move, this);
    // 移除target上所有的注册事件,移除目标上的所有注册事件。所以不是this.node
    // this.node.targetOff(this); 
    },
    /*
    t: --> cc.Touch
    触摸的位置: 屏幕坐标,左小角(0, 0); getLocation();
    */
   on_touch_move: function(t) {
    // 位置
    console.log("cc.Node.EventType.TOUCH_MOVE called");
    console.log(t.getLocation());//触摸的位置
    var w_pos = t.getLocation(); // cc.Vec2 {x, y}
    console.log(w_pos, w_pos.x, w_pos.y);

    // 距离上一次触摸变化了多少;
    var delta = t.getDelta(); // x, y各变化了多少cc.Vec2(x, y)

    this.node.x += delta.x;
    this.node.y += delta.y;
   }, 
   //事件的向上传递
    /*键盘事件
  1:cc.SystemEvent.on(type, function, target, useCapture);
   type: cc.SystemEvent.EventType.KEY_DOWN  按键按下;
         cc.SystemEvent.EventType.KEY_UP 按键弹起; 
2: cc.SystemEvent.on(type, function, target, useCapture);
3: 监听的时候,我们需要一个cc.SystemEvent类的实例,我们有一个全局的实例cc.systemEvent,小写开头
3: 键盘回掉函数: function(event) {
      event.keyCode [cc.KEY.left, ...cc.KEY.xxxx] */

    // (1)向引擎注册一个事件类型的回调函数, 
    // (2) 按键时间的类型: cc.SystemEvent.EventType.KEY_DOWN, cc.SystemEvent.EventType.KEY_UP;
    // (3) 配置的回掉函数: function(event) {} target, 目标
    // (4) 每一个按键,都会对应一个按键码, space, A, B, C, event对象 event.keyCode;
    // (5) cc.systemEvent 小写开头,不是大写, 大写SystemEvent类, systemEvent 全局一个实例

        // 按键被按下
        start:function(){ 
        console.log(cc.systemEvent);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.on_key_down, this);

        // 按键弹起
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.on_key_up, this); 
        },
        
        on_key_down: function(event) {
            switch(event.keyCode) {
                case cc.KEY.space:
                    console.log("space key down!");
                break;
                case cc.KEY.a:
                    console.log("a key down!");
                break;
            }
        },
    
        on_key_up: function(event) {
            switch(event.keyCode) {
                case cc.KEY.space:
                    console.log("space key up!");
                break;
                case cc.KEY.a:
                    console.log("a key up!");
                    break;
            }
        },
 /*自定义事件
1:监听: this.node.on(“自定义事件名称”, function, target, useCapture);
2: 自派送: emit(“事件名称”, [detail]); 只有自己能够收到;
3: 冒泡派送: dispatchEvent(new cc.Event.EventCustom(“name”, 是否冒泡传递));
 */
        // 接收者
        // 事件类型,是自定义的字符串;
        // 回调函数: function(e) {} e--> cc.Event.EventCustom的实例
        //e.detail 自定义参数
    onLoad:function(){

        this.node.on("pkg_event", function(e){
            console.log("pkg_event", e.detail);
        }, this);

        // 派发者,只能传递给自己,不会向上传递
        this.node.emit("pkg_event", {blake: "huang"});
        // end  

        // 派送者,不只是发给自己,发给我们这个体系;
        // true/false, true向上传递, false不向上传递
        var e = new cc.Event.EventCustom("pkg_event", true);//new 出对象
        e.detail = {blake:"uang"};
        this.node.dispatchEvent(e);
        //在父对象节点上查看是否接收信息
    },
});

猜你喜欢

转载自www.cnblogs.com/allyh/p/9961159.html