diff --git a/src/game-lib-system-input.js b/src/game-lib-system-input.js index f16862e..dc68d7a 100644 --- a/src/game-lib-system-input.js +++ b/src/game-lib-system-input.js @@ -72,31 +72,38 @@ GameLib.System.Input.prototype.start = function() { */ if (this.touchControls.length > 0) { - this.touchControls.map( - function(touchControl) { - touchControl.domElement.instance.addEventListener( - 'touchstart', - this.touchStart, - false - ); + this.sensitivityCounter = 0; + + if (this.touchControls.length > 1) { + console.warn('too many touch controls - will use ' + this.touchControls[0].name) + } + + var touchControl = this.touchControls[0]; + + this.touchSensitivity = touchControl.sensitivity; + + touchControl.domElement.instance.addEventListener( + 'touchstart', + this.touchStart, + false + ); + + touchControl.domElement.instance.addEventListener( + 'touchmove', + this.touchMove, + false + ); + touchControl.domElement.instance.addEventListener( + 'touchend', + this.touchEnd, + false + ); + touchControl.domElement.instance.addEventListener( + 'touchcancel', + this.touchCancel, + false + ); - touchControl.domElement.instance.addEventListener( - 'touchmove', - this.touchMove(touchControl), - false - ); - touchControl.domElement.instance.addEventListener( - 'touchend', - this.touchEnd, - false - ); - touchControl.domElement.instance.addEventListener( - 'touchcancel', - this.touchCancel, - false - ); - }.bind(this) - ) } /** @@ -241,63 +248,74 @@ GameLib.System.Input.prototype.onTouchStart = function(event) { ) }; -GameLib.System.Input.prototype.onTouchMove = function(touchControl) { +GameLib.System.Input.prototype.onTouchMove = function (event) { - return function (event) { + this.sensitivityCounter++; - this.sensitivityCounter++; + if (this.sensitivityCounter < this.touchSensitivity) { + /** + * ignore this event + */ + return; + } else { + this.sensitivityCounter = 0; + } - if (this.sensitivityCounter < touchControl.sensitivity) { + for (var t = 0; t < event.changedTouches.length; t++) { - console.log(this.sensitivityCounter); + var id = event.changedTouches[t].identifier; - return; - } else { - this.sensitivityCounter = 0; + var left = 0; + var right = 0; + var up = 0; + var down = 0; + + if (event.changedTouches[t].pageX > this.touches[id].pageX) { + right = 1; + if (this.touches[id].left > 0) { + this.touches[id].left = 0; + this.sensitivityCounter = this.touchSensitivity; + } } - - for (var t = 0; t < event.changedTouches.length; t++) { - - var id = event.changedTouches[t].identifier; - - var left = 0; - var right = 0; - var up = 0; - var down = 0; - - if (event.changedTouches[t].pageX > this.touches[id].pageX) { - right = 1; + if (event.changedTouches[t].pageX < this.touches[id].pageX) { + left = 1; + if (this.touches[id].right > 0) { + this.touches[id].right = 0; + this.sensitivityCounter = this.touchSensitivity; } - - if (event.changedTouches[t].pageX < this.touches[id].pageX) { - left = 1; - } - - if (event.changedTouches[t].pageY < this.touches[id].pageY) { - up = 1; - } - - if (event.changedTouches[t].pageY > this.touches[id].pageY) { - down = 1; - } - - this.touches[id].right += right; - this.touches[id].left += left; - this.touches[id].up += up; - this.touches[id].down += down; - this.touches[id].pageX = event.changedTouches[t].pageX; - this.touches[id].pageY = event.changedTouches[t].pageY; } - this.touches.event = event; + if (event.changedTouches[t].pageY < this.touches[id].pageY) { + up = 1; + if (this.touches[id].down > 0) { + this.touches[id].down = 0; + this.sensitivityCounter = this.touchSensitivity; + } + } - GameLib.Event.Emit( - GameLib.Event.TOUCH_MOVE, - this.touches - ) + if (event.changedTouches[t].pageY > this.touches[id].pageY) { + down = 1; + if (this.touches[id].up > 0) { + this.touches[id].up = 0; + this.sensitivityCounter = this.touchSensitivity; + } + } - }.bind(this); + this.touches[id].right += right; + this.touches[id].left += left; + this.touches[id].up += up; + this.touches[id].down += down; + this.touches[id].pageX = event.changedTouches[t].pageX; + this.touches[id].pageY = event.changedTouches[t].pageY; + } + + this.touches.event = event; + + GameLib.Event.Emit( + GameLib.Event.TOUCH_MOVE, + this.touches + ); };