For the moment it is a test so not clean code.
I can’t test this code for the moment, but if I remove logic code, it should done something like :
var GameLayer = cc.Layer.extend({
isMouseClicked:false,
ctor:function () {
this._super();
this.initTouches();
return true;
},
initTouches: function() {
// Tactil
if( 'touches' in cc.sys.capabilities ) {
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function(touch, event) {
this.isMouseClicked = true;
return this.touchesProcess(touch, event);
},
onTouchMoved: function(touch, event) {
if (this.isMouseClicked) {
this.touchesProcess(touch, event);
}
},
onTouchEnded: function(touch, event) {
this.isMouseClicked = false;
this.touchesProcess(touch, event);
},
onTouchCancelled: function(touch, event) {
this.isMouseClicked = false;
this.touchesProcess(touch, event);
}
}, this);
}
// Mouse
if ('mouse' in cc.sys.capabilities) {
cc.eventManager.addListener({
event: cc.EventListener.MOUSE,
onMouseDown: function(event){
if(event.getButton() == cc.EventMouse.BUTTON_LEFT) {
this.isMouseClicked = true;
event.getCurrentTarget().processMoveEvent(event.getLocation());
}
},
onMouseMove: function(event){
if (this.isMouseClicked) {
event.getCurrentTarget().processMoveEvent(event.getLocation());
}
},
onMouseUp: function(event){
this.isMouseClicked = false;
event.getCurrentTarget().processStartEvent(event.getLocation());
}
}, this);
}
},
touchesProcess: function(touch, event){
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)) {
event.getCurrentTarget().processMoveEvent(locationInNode);
return true;
}
return false;
},
processMoveEvent:function (position) {
// My code when the finger move
}
});
var GameScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new GameLayer();
this.addChild(layer);
}
});
My goal was to convert touches to only have one function for finger/mouse move.
It was working great at the beginning but I make a refactorization of my code and it doesn’t work anymore.
As I have a Kindle Fire 6" I didn’t find the way to debug on it, and
this.touchesProcess is not a function
is the only thing I get on iOS simulator.
I hope it can help you.