From fd0a071cb3c56ba8a759283f455f4c099eb0472c Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 13 Sep 2013 04:37:06 +0100 Subject: [PATCH] Added Sprite.centerOn(x,y) and fixed the InputHander snap as a result. --- examples/drag.php | 43 +++++++++++++++++++++++++++++++ examples/snap.php | 54 +++++++++++++++++++++++++++++++++++++++ src/gameobjects/Sprite.js | 11 ++++++++ src/input/InputHandler.js | 17 ++++++------ 4 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 examples/drag.php create mode 100644 examples/snap.php diff --git a/examples/drag.php b/examples/drag.php new file mode 100644 index 00000000..8ee651eb --- /dev/null +++ b/examples/drag.php @@ -0,0 +1,43 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/snap.php b/examples/snap.php new file mode 100644 index 00000000..bbc78ef4 --- /dev/null +++ b/examples/snap.php @@ -0,0 +1,54 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 1497024f..a9fe40ae 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -313,6 +313,17 @@ Phaser.Sprite.prototype.preUpdate = function() { } +/** + * Moves the sprite so its center is located on the given x and y coordinates. + * Doesn't change the origin of the sprite. + */ +Phaser.Sprite.prototype.centerOn = function(x, y) { + + this.x = x + (this.x - this.center.x); + this.y = y + (this.y - this.center.y); + +} + Phaser.Sprite.prototype.revive = function() { this.alive = true; diff --git a/src/input/InputHandler.js b/src/input/InputHandler.js index 35173284..41be1595 100644 --- a/src/input/InputHandler.js +++ b/src/input/InputHandler.js @@ -649,9 +649,10 @@ Phaser.InputHandler.prototype = { */ enableDrag: function (lockCenter, bringToTop, pixelPerfect, alphaThreshold, boundsRect, boundsSprite) { - lockCenter = lockCenter || false; - bringToTop = bringToTop || false; - pixelPerfect = pixelPerfect || false; + if (typeof lockCenter == 'undefined') { lockCenter = false; } + if (typeof bringToTop == 'undefined') { bringToTop = false; } + if (typeof pixelPerfect == 'undefined') { pixelPerfect = false; } + alphaThreshold = alphaThreshold || 255; boundsRect = boundsRect || null; boundsSprite = boundsSprite || null; @@ -707,7 +708,7 @@ Phaser.InputHandler.prototype = { if (this.dragFromCenter) { - // this.sprite.transform.centerOn(pointer.worldX, pointer.worldY); + this.sprite.centerOn(pointer.x, pointer.y); this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y); } else @@ -773,13 +774,13 @@ Phaser.InputHandler.prototype = { */ enableSnap: function (snapX, snapY, onDrag, onRelease) { - onDrag = onDrag || true; - onRelease = onRelease || false; + if (typeof onDrag == 'undefined') { onDrag = true; } + if (typeof onRelease == 'undefined') { onRelease = false; } - this.snapOnDrag = onDrag; - this.snapOnRelease = onRelease; this.snapX = snapX; this.snapY = snapY; + this.snapOnDrag = onDrag; + this.snapOnRelease = onRelease; },