![](https://img-blog.csdn.net/20160617184514248?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
var that = this;
var listener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
/*var target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(touch.getLocation());*/
cc.log(123)
var locationInNode= touch.getLocation();
//var s = target.getContentSize();
for(var index in that.cellArr){
var cell=that.cellArr[index]
var rect = cc.rect(cell.x,cell.y , cell.width, cell.height);
if (cc.rectContainsPoint(rect, locationInNode)){
if(cell.getSelected()){
cell.setSelected(false)
that._temp = null
}
else{
cell.setSelected(true)
if(that._temp == null){
that._temp = cell
}
else {
var cengjingdeI = that.cellArr.indexOf(that._temp)
that.cellArr[cengjingdeI].setSelected(false)
cell.setSelected(false)
var temp = cell.getPosition()
cell.setPosition(that.cellArr[cengjingdeI].getPosition())
that.cellArr[cengjingdeI].setPosition(temp)
that._temp = null
}
}
return
}
}
}
});
cc.eventManager.addListener(listener, this);
},
其中的onTouchBegan是固定的api 是不能自己定义的 ,
cc.eventManager.addListener(listener, this); 这是创建一个点击事件 其中listener是点击的方法。而this是传入的是类的对象。
点击对象互换的功能:
var cengjingdeI = that.cellArr.indexOf(that._temp)
that.cellArr[cengjingdeI].setSelected(false)
cell.setSelected(false)
var temp = cell.getPosition()
cell.setPosition(that.cellArr[cengjingdeI].getPosition())
that.cellArr[cengjingdeI].setPosition(temp)
that._temp = null
这是在数组里面存了若干个小的对象 或者可以是图片 当点击时获得当前点击的坐标
然后定义一个变量计算是否有点击。