From b77c034f61e89d387f8f258367f5888b3aec3515 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 16 Apr 2014 22:59:19 +0100 Subject: [PATCH] World.wrap will take a game object and if its x/y coordinates fall outside of the world bounds it will be repositioned on the opposite side, for a wrap-around effect. --- README.md | 3 ++- src/core/World.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 652730ac2..ea203ea36 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,12 @@ Version 2.0.4 - "Mos Shirare" - in development * Key.reset has a new `hard` parameter which controls the severity of the reset. A soft reset doesn't remove any callbacks or event listeners. * InputManager.resetLocked - If the Input Manager has been reset locked then all calls made to InputManager.reset, such as from a State change, are ignored. * Group.resetCursor will reset the Group cursor back to the start of the group, or to the given index value. +* World.wrap will take a game object and if its x/y coordinates fall outside of the world bounds it will be repositioned on the opposite side, for a wrap-around effect. ### Bug Fixes -* The main Timer loop could incorrectly remove TimeEvent if a new one was added specifically during an event callback (thanks @garyyeap, fix #710) +* The main Timer loop could incorrectly remove a TimerEvent if a new one was added specifically during an event callback (thanks @garyyeap, fix #710) * Fixed the use of the destroy parameter in Group.removeAll and related functions (thanks @AnderbergE, fix #717) * P2.World.convertTilemap now correctly checks the collides parameter of the tiles as it converts them. * Animation.destroy didn't correctly clear the onStart, onLoop and onComplete signals. diff --git a/src/core/World.js b/src/core/World.js index c6d039250..bcf3d4a03 100644 --- a/src/core/World.js +++ b/src/core/World.js @@ -104,6 +104,65 @@ Phaser.World.prototype.shutdown = function () { }; +/** +* This will take the given game object and check if its x/y coordinates fall outside of the world bounds. +* If they do it will reposition the object to the opposite side of the world, creating a wrap-around effect. +* +* @method Phaser.World#wrap +* @param {Phaser.Sprite|Phaser.Image|Phaser.TileSprite|Phaser.Text} sprite - The object you wish to wrap around the world bounds. +* @param {number} [padding=0] - Extra padding added equally to the sprite.x and y coordinates before checking if within the world bounds. Ignored if useBounds is true. +* @param {boolean} [useBounds=false] - If useBounds is false wrap checks the object.x/y coordinates. If true it does a more accurate bounds check, which is more expensive. +*/ +Phaser.World.prototype.wrap = function (sprite, padding, useBounds) { + + if (typeof padding === 'undefined') { padding = 0; } + if (typeof useBounds === 'undefined') { useBounds = false; } + + if (!useBounds) + { + if (sprite.x + padding < this.bounds.x) + { + sprite.x = this.bounds.right + padding; + } + else if (sprite.x - padding > this.bounds.right) + { + sprite.x = this.bounds.left - padding; + } + + if (sprite.y + padding < this.bounds.top) + { + sprite.y = this.bounds.bottom + padding; + } + else if (sprite.y - padding > this.bounds.bottom) + { + sprite.y = this.bounds.top - padding; + } + } + else + { + sprite.getBounds(); + + if (sprite._currentBounds.right < this.bounds.x) + { + sprite.x = this.bounds.right; + } + else if (sprite._currentBounds.x > this.bounds.right) + { + sprite.x = this.bounds.left; + } + + if (sprite._currentBounds.bottom < this.bounds.top) + { + sprite.y = this.bounds.bottom; + } + else if (sprite._currentBounds.top > this.bounds.bottom) + { + sprite.y = this.bounds.top; + } + } + +}; + /** * @name Phaser.World#width * @property {number} width - Gets or sets the current width of the game world.