//创建一个事件监听器 OneByOne
var listener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true, //设置是否吞没事件,在onTouchBegan方法返回true时吞掉事件,不再向下传递。
onTouchBegan: function (touch, event) { //实现onTouchBegan事件处理回调函数
var target = event.getCurrentTarget(); //获取事件所绑定的target, 通常是cc.Node及其子类
//获取当前触摸点相对于按钮所在的坐标
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)) {
cc.log("sprite began... x = " + locationInNode.x + ", y = " + locationInNode.y);
target.opacity = 180;
return true;
}
return false;
},
onTouchMoved: function (touch, event) { //实现onTouchMoved事件处理回调函数, 触摸移动时触发
// 移动当前按钮精灵的坐标位置
var target = event.getCurrentTarget();
var delta = touch.getDelta(); //获取事件数据:delta
target.x += delta.x;
target.y += delta.y;
},
onTouchEnded: function (touch, event) { //实现onTouchEnded事件处理回调函数
var target = event.getCurrentTarget();
cc.log("sprite onTouchesEnded.. ");
target.setOpacity(255);
}
});
cc.eventManager.addListener(listener, this);