From 494c33a9f4b2b144e516c74bd06318932c62574d Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 9 Jul 2014 11:34:18 +0100 Subject: [PATCH] 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. --- README.md | 2 ++ build/phaser.d.ts | 2 ++ src/geom/Rectangle.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/README.md b/README.md index e731653f9..5bd4e6caf 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,8 @@ Version 2.0.6 - "Jornhill" - -in development- * Phaser.Utils.mixin will mix the source object into the destination object, returning the newly modified destination object. * You can now use `game.add.plugin` from the GameObjectFactory (thanks @alvinsight, #978) * Color.getWebRGB will now accept either an Object or numeric color value. +* 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. ### Bug Fixes diff --git a/build/phaser.d.ts b/build/phaser.d.ts index 48e3d05dc..ecc6101d8 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -4104,6 +4104,8 @@ declare module Phaser { height: number; left: number; perimeter: number; + randomX: number; + randomY: number; right: number; top: number; topLeft: Phaser.Point; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js index ff122f668..5f4caf368 100644 --- a/src/geom/Rectangle.js +++ b/src/geom/Rectangle.js @@ -463,6 +463,38 @@ Object.defineProperty(Phaser.Rectangle.prototype, "centerY", { }); +/** +* A random value between the left and right values (inclusive) of the Rectangle. +* +* @name Phaser.Rectangle#randomX +* @property {number} randomX - A random value between the left and right values (inclusive) of the Rectangle. +*/ +Object.defineProperty(Phaser.Rectangle.prototype, "randomX", { + + get: function () { + + return this.x + (Math.random() * this.width); + + } + +}); + +/** +* A random value between the top and bottom values (inclusive) of the Rectangle. +* +* @name Phaser.Rectangle#randomY +* @property {number} randomY - A random value between the top and bottom values (inclusive) of the Rectangle. +*/ +Object.defineProperty(Phaser.Rectangle.prototype, "randomY", { + + get: function () { + + return this.y + (Math.random() * this.height); + + } + +}); + /** * 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. * However it does affect the height property, whereas changing the y value does not affect the height property.