1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| function addTouchToNode(node, touchEndCall, target, params) { //给node添加触摸事件 var bTouchCanceled = false; var s = node.getContentSize(); var rect = cc.rect(0, 0, s.width, s.height); var listener = node._tyTouchListener = cc.EventListener.create({ event: cc.EventListener.TOUCH_ONE_BY_ONE, swallowTouches: true, onTouchBegan: function(touch, event) { var locationInNode = node.convertToNodeSpace(touch.getLocation()); if (cc.rectContainsPoint(rect, locationInNode)) { bTouchCanceled = false; return true; } return false; }, onTouchEnded: function(touch, event) { if (bTouchCanceled) { return; } var locationInNode = node.convertToNodeSpace(touch.getLocation()); if (cc.rectContainsPoint(rect, locationInNode)) { if (touchEndCall) { touchEndCall.call(target, locationInNode, params); } } }, onTouchCancelled: function() { bTouchCanceled = true; },
onTouchMoved: function(touch, event) { var locationInNode = node.convertToNodeSpace(touch.getLocation()); if (cc.rectContainsPoint(rect, locationInNode) == false) { bTouchCanceled = true; } } }); cc.eventManager.addListener(listener, node); node.onEnter = function(){ node.__proto__.onEnter.call(this); var listener = this._tyTouchListener; if(listener._isRegistered() === false) { cc.eventManager.addListener(listener, this); } }; };
|