Use a custom bounds rectangle for the Arcade Body to overwrite the world bounds

This commit is contained in:
Francois 2018-12-15 22:04:21 +01:00
parent 83f3f9cdeb
commit 7033297aa3

View file

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