cc.Node—事件响应

触摸事件
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: 完成物体跟随手指触摸的案例;

 1         /*
 2          (1) 监听对应的触摸事件: 像引擎底层注册一个回掉函数,当有触摸事件发生的时候掉这个回掉函数;
 3          cc.Node.EventType.TOUCH_START: 触摸开始
 4          cc.Node.EventType.TOUCH_MOVE: 触摸移动
 5          cc.Node.EventType.TOUCH_END: 触摸结束, (物体内部结束)
 6          cc.Node.EventType.TOUCH_CANCEL: 触摸结束, (物体外部结束)
 7         
 8         (2) 回掉函数的格式  function(t)  --> cc.Touch对象触摸事件对象 {触摸信息,事件信息}
 9         call --> this, this指向谁就是这个target;你要绑那个对象作为你回掉函数的this, 可以为空
10         function () {}.bind(this);         
11         */
12         var item = this.node.getChildByName("item");
13 
14         this.node.on(cc.Node.EventType.TOUCH_START,
15             function(t) {
16                 console.log("cc.Node.EventType.TOUCH_START called,", t.type);
17                 // this 函数里面的this,
18                 // 停止事件传递
19                 t.stopPropagationImmediate();
20             }, item);
21 
22         this.node.on(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move, item);
23 
24         this.node.on(cc.Node.EventType.TOUCH_END,
25             function(t) {
26                 console.log("cc.Node.EventType.TOUCH_END called,", t.type);
27             },
28             item);
29 
30         this.node.on(cc.Node.EventType.TOUCH_CANCEL,
31             function(t) {
32                 console.log("cc.Node.EventType.TOUCH_CANCEL called,", t.type);
33             },
34             item);
35 
36         // 移除
37         this.node.off(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move, this);
38         // 移除target上所有的注册事件
39         this.node.targetOff(this);

父级节点监听事件

1     onLoad: function () {
2         this.node.on("pkg_event", function(e){
3             console.log("父级监听事件", e.type, e.detail);
4         }, this);

按键监听

 1     // (1)向引擎注册一个事件类型的回掉函数, 
 2     // (2) 按键时间的类型: cc.SystemEvent.EventType.KEY_DOWN, cc.SystemEvent.EventType.KEY_UP;
 3     // (3) 配置的回掉函数: function(event) {} target, 目标
 4     // (4) 每一个按键,都会对应一个按键码, space, A, B, C, event对象 event.keyCode;
 5     // (5) cc.systemEvent 小写开头,不是大写, 大写SystemEvent类, systemEvent 全局一个实例
 6     onLoad: function () {
 7         // console.log(cc.systemEvent);
 8         // 按键被按下
 9         cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.on_key_down, this);
10 
11         // 按键弹起
12         cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.on_key_up, this);
13     },
14 
15     on_key_down: function (event) {
16         console.log('key down:%s', event.keyCode);
17     },
18 
19     on_key_up: function (event) {
20         console.log('key up:', event.keyCode);
21     },

自定义事件

 1     onLoad: function () {
 2         console.log('this is onload..');
 3         // 接收者
 4         // 事件类型,是你自定义的字符串;
 5         // 回掉函数: function(e) {} e--> cc.Event.EventCustom的实例
 6         this.node.on("pkg_event", function(e){
 7             console.log("自定义事件", e.detail);
 8         }, this);
 9         // end
10 
11         this.node.emit("pkg_event", { blake: "发射" });
12       
13 
14     },
15 
16     start: function () {
17         console.log('start...');
18         // 派发者,只能传递给自己,不会向上传递
19         this.node.emit("pkg_event", {blake: "发射狗子"});
20 
21         // 派送者,不只是发给自己,发给我们这个体系;
22         // true/false, true向上传递, false不向上传递
23         var e = new cc.Event.EventCustom("pkg_event", false);
24         e.detail = {blake: "派送分发"};
25         this.node.dispatchEvent(e);
26     },
原文地址:https://www.cnblogs.com/he-bo/p/9723999.html