Added Sprite.centerOn(x,y) and fixed the InputHander snap as a result.

This commit is contained in:
Richard Davey 2013-09-13 04:37:06 +01:00
parent 62d77e7038
commit fd0a071cb3
4 changed files with 117 additions and 8 deletions

43
examples/drag.php Normal file
View file

@ -0,0 +1,43 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
function preload() {
game.load.image('grid', 'assets/tests/debug-grid-1920x1920.png');
game.load.image('atari', 'assets/sprites/atari800xl.png');
}
function create() {
game.add.sprite(0, 0, 'grid');
atari1 = game.add.sprite(300, 300, 'atari');
// Input Enable the sprites
atari1.inputEnabled = true;
// Allow dragging
// enableDrag parameters = (lockCenter, bringToTop, pixelPerfect, alphaThreshold, boundsRect, boundsSprite)
atari1.input.enableDrag(true);
}
})();
</script>
</body>
</html>

54
examples/snap.php Normal file
View file

@ -0,0 +1,54 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
function preload() {
game.load.image('grid', 'assets/tests/debug-grid-1920x1920.png');
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('atari2', 'assets/sprites/atari800xl.png');
}
function create() {
game.add.sprite(0, 0, 'grid');
atari1 = game.add.sprite(128, 128, 'atari1');
atari2 = game.add.sprite(256, 256, 'atari2');
// Input Enable the sprites
atari1.inputEnabled = true;
atari2.inputEnabled = true;
// Allow dragging
// enableDrag parameters = (lockCenter, bringToTop, pixelPerfect, alphaThreshold, boundsRect, boundsSprite)
atari1.input.enableDrag();
atari2.input.enableDrag();
// Enable snapping. For the atari1 sprite it will snap as its dragged around and on release.
// The snap is set to every 32x32 pixels.
atari1.input.enableSnap(32, 32, true, true);
// For the atari2 sprite it will snap only when released, not on drag.
atari2.input.enableSnap(32, 32, false, true);
}
})();
</script>
</body>
</html>

View file

@ -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;

View file

@ -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;
},