Sprites that are fixedToCamera can now be input dragged regardless of world position.

This commit is contained in:
photonstorm
2013-12-22 03:46:08 +00:00
parent 3cbb820349
commit 63d90a0176
7 changed files with 170 additions and 33 deletions
+1
View File
@@ -106,6 +106,7 @@ Bug Fixes:
* Fixed an issue where passing null as the Group parent wouldn't set it to game.world as it should have (thanks tito100)
* Fixed Pixi bug (#425) incorrect width property for multi-line BitmapText (thanks jcd-as)
* Tween.onStart is now called when the tween starts AFTER the delay value, if given (thanks stevenbouma)
* Sprites that are fixedToCamera can now be input dragged regardless of world position (thanks RafaelOliveira)
You can view the Change Log for all previous versions at https://github.com/photonstorm/phaser/changelog.md
+72
View File
@@ -0,0 +1,72 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.stage.backgroundColor = '#007236';
game.load.image('ball', 'assets/sprites/shinyball.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('phaser', 'assets/sprites/sonic_havok_sanity.png');
}
var sprite;
var cursors;
function create() {
game.world.setBounds(0, 0, 1000, 1000);
for (var i = 0; i < 100; i++)
{
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
}
sprite = game.add.sprite(200, 200, 'ball');
sprite.fixedToCamera = true;
// sprite.cameraOffset.setTo(200, 200);
sprite.inputEnabled = true;
sprite.input.enableDrag();
sprite.events.onInputOver.add(wibble, this);
cursors = game.input.keyboard.createCursorKeys();
}
function wibble() {
console.log('over');
}
function update() {
if (cursors.up.isDown)
{
game.camera.y -= 4;
}
else if (cursors.down.isDown)
{
game.camera.y += 4;
}
if (cursors.left.isDown)
{
game.camera.x -= 4;
}
else if (cursors.right.isDown)
{
game.camera.x += 4;
}
}
function render() {
game.debug.renderText(sprite.cameraOffset.x, 32, 32);
game.debug.renderText(sprite.cameraOffset.y, 32, 64);
game.debug.renderPointer(game.input.activePointer);
}
+1 -1
View File
@@ -28,7 +28,7 @@ var module;
function create() {
module = new Protracker();
module.buffer = game.cache.getBinary('elysium');
module.buffer = game.cache.getBinary('impulse');
module.parse();
module.play();
+5 -4
View File
@@ -50,13 +50,14 @@ function create() {
// map.setCollisionByIndexRange(20, 25);
// map.setCollisionByIndexRange(27, 29);
layer3 = map.createLayer('Tile Layer 3');
layer3.scrollFactorX = 0.5;
// layer3 = map.createLayer('Tile Layer 3');
// layer3.scrollFactorX = 0.5;
layer2 = map.createLayer('Tile Layer 2');
layer2.alpha = 0.5;
// layer2 = map.createLayer('Tile Layer 2');
// layer2.alpha = 0.5;
layer = map.createLayer('Tile Layer 1');
layer.scrollFactorX = 0.5;
// layer.debug = true;
+12 -3
View File
@@ -354,7 +354,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
/**
* @property {Phaser.Point} cameraOffset - If this Sprite is fixed to the camera then use this Point to specify how far away from the Camera x/y it's rendered.
*/
this.cameraOffset = new Phaser.Point();
this.cameraOffset = new Phaser.Point(x, y);
/**
* You can crop the Sprites texture by modifying the crop properties. For example crop.width = 50 would set the Sprite to only render 50px wide.
@@ -772,8 +772,17 @@ Phaser.Sprite.prototype.loadTexture = function (key, frame) {
*/
Phaser.Sprite.prototype.centerOn = function(x, y) {
this.x = x + (this.x - this.center.x);
this.y = y + (this.y - this.center.y);
if (this.fixedToCamera)
{
this.cameraOffset.x = x + (this.cameraOffset.x - this.center.x);
this.cameraOffset.y = y + (this.cameraOffset.y - this.center.y);
}
else
{
this.x = x + (this.x - this.center.x);
this.y = y + (this.y - this.center.y);
}
return this;
};
+78 -24
View File
@@ -701,30 +701,61 @@ Phaser.InputHandler.prototype = {
return false;
}
if (this.allowHorizontalDrag)
if (this.sprite.fixedToCamera)
{
this.sprite.x = pointer.x + this._dragPoint.x + this.dragOffset.x;
}
if (this.allowHorizontalDrag)
{
this.sprite.cameraOffset.x = pointer.x + this._dragPoint.x + this.dragOffset.x;
}
if (this.allowVerticalDrag)
{
this.sprite.y = pointer.y + this._dragPoint.y + this.dragOffset.y;
}
if (this.allowVerticalDrag)
{
this.sprite.cameraOffset.y = pointer.y + this._dragPoint.y + this.dragOffset.y;
}
if (this.boundsRect)
{
this.checkBoundsRect();
}
if (this.boundsRect)
{
this.checkBoundsRect();
}
if (this.boundsSprite)
{
this.checkBoundsSprite();
}
if (this.boundsSprite)
{
this.checkBoundsSprite();
}
if (this.snapOnDrag)
if (this.snapOnDrag)
{
this.sprite.cameraOffset.x = Math.round(this.sprite.x / this.snapX) * this.snapX;
this.sprite.cameraOffset.y = Math.round(this.sprite.y / this.snapY) * this.snapY;
}
}
else
{
this.sprite.x = Math.round(this.sprite.x / this.snapX) * this.snapX;
this.sprite.y = Math.round(this.sprite.y / this.snapY) * this.snapY;
if (this.allowHorizontalDrag)
{
this.sprite.x = pointer.x + this._dragPoint.x + this.dragOffset.x;
}
if (this.allowVerticalDrag)
{
this.sprite.y = pointer.y + this._dragPoint.y + this.dragOffset.y;
}
if (this.boundsRect)
{
this.checkBoundsRect();
}
if (this.boundsSprite)
{
this.checkBoundsSprite();
}
if (this.snapOnDrag)
{
this.sprite.x = Math.round(this.sprite.x / this.snapX) * this.snapX;
this.sprite.y = Math.round(this.sprite.y / this.snapY) * this.snapY;
}
}
return true;
@@ -904,14 +935,29 @@ Phaser.InputHandler.prototype = {
this._draggedPointerID = pointer.id;
this._pointerData[pointer.id].isDragged = true;
if (this.dragFromCenter)
if (this.sprite.fixedToCamera)
{
this.sprite.centerOn(pointer.x, pointer.y);
this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y);
if (this.dragFromCenter)
{
this.sprite.centerOn(pointer.x, pointer.y);
this._dragPoint.setTo(this.sprite.cameraOffset.x - pointer.x, this.sprite.cameraOffset.y - pointer.y);
}
else
{
this._dragPoint.setTo(this.sprite.cameraOffset.x - pointer.x, this.sprite.cameraOffset.y - pointer.y);
}
}
else
{
this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y);
if (this.dragFromCenter)
{
this.sprite.centerOn(pointer.x, pointer.y);
this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y);
}
else
{
this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y);
}
}
this.updateDrag(pointer);
@@ -938,8 +984,16 @@ Phaser.InputHandler.prototype = {
if (this.snapOnRelease)
{
this.sprite.x = Math.round(this.sprite.x / this.snapX) * this.snapX;
this.sprite.y = Math.round(this.sprite.y / this.snapY) * this.snapY;
if (this.sprite.fixedToCamera)
{
this.sprite.cameraOffset.x = Math.round(this.sprite.cameraOffset.x / this.snapX) * this.snapX;
this.sprite.cameraOffset.y = Math.round(this.sprite.cameraOffset.y / this.snapY) * this.snapY;
}
else
{
this.sprite.x = Math.round(this.sprite.x / this.snapX) * this.snapX;
this.sprite.y = Math.round(this.sprite.y / this.snapY) * this.snapY;
}
}
this.sprite.events.onDragStop.dispatch(this.sprite, pointer);
+1 -1
View File
@@ -304,7 +304,7 @@ Phaser.TilemapLayer.prototype.postUpdate = function () {
*/
Phaser.TilemapLayer.prototype.resizeWorld = function () {
this.game.world.setBounds(0, 0, this.layer.widthInPixels, this.layer.heightInPixels);
this.game.world.setBounds(0, 0, this.layer.widthInPixels * 4, this.layer.heightInPixels);
}