Skip to content

Commit 494c33a

Browse files
committed
Rectangle.randomX will return a random value located within the horizontal bounds of the Rectangle.
Rectangle.randomY will return a random value located within the vertical bounds of the Rectangle.
1 parent 003403c commit 494c33a

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Version 2.0.6 - "Jornhill" - -in development-
134134
* Phaser.Utils.mixin will mix the source object into the destination object, returning the newly modified destination object.
135135
* You can now use `game.add.plugin` from the GameObjectFactory (thanks @alvinsight, #978)
136136
* Color.getWebRGB will now accept either an Object or numeric color value.
137+
* Rectangle.randomX will return a random value located within the horizontal bounds of the Rectangle.
138+
* Rectangle.randomY will return a random value located within the vertical bounds of the Rectangle.
137139

138140

139141
### Bug Fixes

build/phaser.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,6 +4104,8 @@ declare module Phaser {
41044104
height: number;
41054105
left: number;
41064106
perimeter: number;
4107+
randomX: number;
4108+
randomY: number;
41074109
right: number;
41084110
top: number;
41094111
topLeft: Phaser.Point;

src/geom/Rectangle.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,38 @@ Object.defineProperty(Phaser.Rectangle.prototype, "centerY", {
463463

464464
});
465465

466+
/**
467+
* A random value between the left and right values (inclusive) of the Rectangle.
468+
*
469+
* @name Phaser.Rectangle#randomX
470+
* @property {number} randomX - A random value between the left and right values (inclusive) of the Rectangle.
471+
*/
472+
Object.defineProperty(Phaser.Rectangle.prototype, "randomX", {
473+
474+
get: function () {
475+
476+
return this.x + (Math.random() * this.width);
477+
478+
}
479+
480+
});
481+
482+
/**
483+
* A random value between the top and bottom values (inclusive) of the Rectangle.
484+
*
485+
* @name Phaser.Rectangle#randomY
486+
* @property {number} randomY - A random value between the top and bottom values (inclusive) of the Rectangle.
487+
*/
488+
Object.defineProperty(Phaser.Rectangle.prototype, "randomY", {
489+
490+
get: function () {
491+
492+
return this.y + (Math.random() * this.height);
493+
494+
}
495+
496+
});
497+
466498
/**
467499
* The y coordinate of the top of the Rectangle. Changing the top property of a Rectangle object has no effect on the x and width properties.
468500
* However it does affect the height property, whereas changing the y value does not affect the height property.

0 commit comments

Comments
 (0)