Skip to content

Commit b77c034

Browse files
committed
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.
1 parent 089dfbb commit b77c034

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ Version 2.0.4 - "Mos Shirare" - in development
7676
* 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.
7777
* 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.
7878
* Group.resetCursor will reset the Group cursor back to the start of the group, or to the given index value.
79+
* 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.
7980

8081

8182
### Bug Fixes
8283

83-
* The main Timer loop could incorrectly remove TimeEvent if a new one was added specifically during an event callback (thanks @garyyeap, fix #710)
84+
* The main Timer loop could incorrectly remove a TimerEvent if a new one was added specifically during an event callback (thanks @garyyeap, fix #710)
8485
* Fixed the use of the destroy parameter in Group.removeAll and related functions (thanks @AnderbergE, fix #717)
8586
* P2.World.convertTilemap now correctly checks the collides parameter of the tiles as it converts them.
8687
* Animation.destroy didn't correctly clear the onStart, onLoop and onComplete signals.

src/core/World.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,65 @@ Phaser.World.prototype.shutdown = function () {
104104

105105
};
106106

107+
/**
108+
* This will take the given game object and check if its x/y coordinates fall outside of the world bounds.
109+
* If they do it will reposition the object to the opposite side of the world, creating a wrap-around effect.
110+
*
111+
* @method Phaser.World#wrap
112+
* @param {Phaser.Sprite|Phaser.Image|Phaser.TileSprite|Phaser.Text} sprite - The object you wish to wrap around the world bounds.
113+
* @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.
114+
* @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.
115+
*/
116+
Phaser.World.prototype.wrap = function (sprite, padding, useBounds) {
117+
118+
if (typeof padding === 'undefined') { padding = 0; }
119+
if (typeof useBounds === 'undefined') { useBounds = false; }
120+
121+
if (!useBounds)
122+
{
123+
if (sprite.x + padding < this.bounds.x)
124+
{
125+
sprite.x = this.bounds.right + padding;
126+
}
127+
else if (sprite.x - padding > this.bounds.right)
128+
{
129+
sprite.x = this.bounds.left - padding;
130+
}
131+
132+
if (sprite.y + padding < this.bounds.top)
133+
{
134+
sprite.y = this.bounds.bottom + padding;
135+
}
136+
else if (sprite.y - padding > this.bounds.bottom)
137+
{
138+
sprite.y = this.bounds.top - padding;
139+
}
140+
}
141+
else
142+
{
143+
sprite.getBounds();
144+
145+
if (sprite._currentBounds.right < this.bounds.x)
146+
{
147+
sprite.x = this.bounds.right;
148+
}
149+
else if (sprite._currentBounds.x > this.bounds.right)
150+
{
151+
sprite.x = this.bounds.left;
152+
}
153+
154+
if (sprite._currentBounds.bottom < this.bounds.top)
155+
{
156+
sprite.y = this.bounds.bottom;
157+
}
158+
else if (sprite._currentBounds.top > this.bounds.bottom)
159+
{
160+
sprite.y = this.bounds.top;
161+
}
162+
}
163+
164+
};
165+
107166
/**
108167
* @name Phaser.World#width
109168
* @property {number} width - Gets or sets the current width of the game world.

0 commit comments

Comments
 (0)