代码如下:
var canvas = cc.director.getScene().getChildByName('Canvas');
var self = this;
if (!this.cursor) {
cc.loader.loadRes(resList.diamondPrefab, function (err, prefab) {
self.cursor = cc.instantiate(prefab);
var startLoc = canvas.convertToNodeSpaceAR(self.node.getPosition());
self.cursor.setPosition(startLoc);
canvas.addChild(self.cursor);
});
//注册鼠标移动的监听
canvas.on(cc.Node.EventType.MOUSE_MOVE, function (touch) {
cc.log('监听中');
if (self.cursor) {
var touchLoc = touch.getLocation();
touchLoc = canvas.convertToNodeSpaceAR(touchLoc);
self.cursor.setPosition(touchLoc);
}
}, this);
} else {
this.cursor.removeFromParent();
this.cursor = null;
cc.log('移除监听');
//注销鼠标移动的监听
canvas.off(cc.Node.EventType.MOUSE_MOVE, function (touch){
cc.log('移除监听');
}, this);
}
注销监听调用成功,但鼠标移动的监听仍在,求大神指点