From 7033297aa3b99c1d9fc0f7a8c3b9fc8006eae807 Mon Sep 17 00:00:00 2001 From: Francois Date: Sat, 15 Dec 2018 22:04:21 +0100 Subject: [PATCH] Use a custom bounds rectangle for the Arcade Body to overwrite the world bounds --- src/physics/arcade/Body.js | 43 +++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index 524c23dde..256e07a92 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -392,6 +392,17 @@ var Body = new Class({ */ this.worldBounce = null; + /** + * The custom boundary rectangle to use instead of the world boundary. + * If null, the world boundaries are used instead. + * + * @name Phaser.Physics.Arcade.Body#customBoundsRectangle + * @type {?Phaser.Geom.Rectangle} + * @default null + * @since 3.16.1 + */ + this.customBoundsRectangle = null; + // If true this Body will dispatch events /** @@ -1026,6 +1037,36 @@ var Body = new Class({ this.prev.y = this.position.y; }, + /** + * Returns the collision boundary rectangle. Either a custom one, which was + * set with setBoundsRectangle or the default world's bounds. + * + * @method Phaser.Physics.Arcade.Body#getBoundsRectangle + * @since 3.16.1 + * + * @return {Phaser.Geom.Rectangle} + */ + getBoundsRectangle: function () + { + return (this.customBoundsRectangle || this.world.bounds); + }, + + /** + * Sets a custom collision boundary rectangle. Use if you want to have a custom + * boundary instead of the world boundaries. + * + * @method Phaser.Physics.Arcade.Body#setBoundsRectangle + * @since 3.16.1 + * + * @param {Phaser.Geom.Rectangle} rect - The new boundary rectangle. Pass null to use the default world bounds again. + * @return {Phaser.Physics.Arcade.Body} This Body object. + */ + setBoundsRectangle: function (rect) + { + this.customBoundsRectangle = rect; + return this; + }, + /** * Checks for collisions between this Body and the world boundary and separates them. * @@ -1037,7 +1078,7 @@ var Body = new Class({ checkWorldBounds: function () { var pos = this.position; - var bounds = this.world.bounds; + var bounds = this.getBoundsRectangle(); var check = this.world.checkCollision; var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;