var that = this;
var listener = cc.EventListener.create({ event: cc.EventListener.TOUCH_ONE_BY_ONE, swallowTouches: true,//吞没事件 onTouchBegan: function (touch, event) { if (that.reachTarget) return false; var target = event.getCurrentTarget();//取得所有的侦听事件对象 var locationInNode = target.convertToNodeSpace(touch.getLocation()); var s = target.getContentSize(); var rect = cc.rect(0, 0, s.width, s.height); if (cc.rectContainsPoint(rect, locationInNode)) { target.zIndex = 80; //更改Z轴的值 使得点击在最上 return true; } return false; }, onTouchMoved: function (touch, event) { var target = event.getCurrentTarget(); var delta = touch.getDelta(); target.x += delta.x; target.y += delta.y; }, onTouchEnded: function (touch, event) { var target = event.getCurrentTarget(); if (cc.pDistance(target.getPosition(), that.targetPoint) <80) { //计算2点之间的距离 实现吸附功能 target.setPosition(that.targetPoint); target.zIndex = 10; that.reachTarget = true; that.callBackFun() } } }) cc.eventManager.addListener(listener.clone(),this);