Skip to content

Commit eec9f70

Browse files
committed
Math.removeRandom allows you to remove (and return) a random object from an array.
Updated TypeScript defs to fix getRandom (fix phaserjs#583)
1 parent 870d534 commit eec9f70

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Updated:
8585
New Features:
8686

8787
* Device.getUserMedia boolean added, useful if you need access to the webcam or microphone.
88+
* Math.removeRandom allows you to remove (and return) a random object from an array.
8889

8990

9091
TODO:

build/phaser.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2431,7 +2431,7 @@ declare module Phaser {
24312431
static fuzzyFloor(val: number, epsilon?: number): boolean;
24322432
static fuzzyGreaterThan(a: number, b: number, epsilon?: number): boolean;
24332433
static fuzzyLessThan(a: number, b: number, epsilon?: number): boolean;
2434-
static getRandom(objects: Object[], startIndex?: number, length?: number): Object;
2434+
static getRandom<T>(objects: T[], startIndex?: number, length?: number): T;
24352435
static interpolateAngles(a1: number, a2: number, weight: number, radians?: boolean, ease?: any): number;
24362436
static interpolateFloat(a: number, b: number, weight: number): number;
24372437
static isEven(n: number): boolean;
@@ -2454,6 +2454,7 @@ declare module Phaser {
24542454
static PI2: number;
24552455
static radToDeg(radians: number): number;
24562456
static randomSign(): number;
2457+
static removeRandom<T>(objects: T[], startIndex?: number, length?: number): T;
24572458
static reverseAngle(angleRed: number): number;
24582459
static roundTo(value: number, place?: number, base?: number): number;
24592460
static shear(n: number): number;

src/math/Math.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,42 @@ Phaser.Math = {
981981

982982
},
983983

984+
/**
985+
* Removes a random object from the given array and returns it.
986+
* Will return null if random selection is missing, or array has no entries.
987+
*
988+
* @method Phaser.Math#removeRandom
989+
* @param {array} objects - An array of objects.
990+
* @param {number} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
991+
* @param {number} length - Optional restriction on the number of values you want to randomly select from.
992+
* @return {object} The random object that was removed.
993+
*/
994+
removeRandom: function (objects, startIndex, length) {
995+
996+
if (typeof startIndex === "undefined") { startIndex = 0; }
997+
if (typeof length === "undefined") { length = 0; }
998+
999+
if (objects != null) {
1000+
1001+
var l = length;
1002+
1003+
if ((l === 0) || (l > objects.length - startIndex))
1004+
{
1005+
l = objects.length - startIndex;
1006+
}
1007+
1008+
if (l > 0)
1009+
{
1010+
var idx = startIndex + Math.floor(Math.random() * l);
1011+
var removed = objects.splice(idx, 1);
1012+
return removed[0];
1013+
}
1014+
}
1015+
1016+
return null;
1017+
1018+
},
1019+
9841020
/**
9851021
* Round down to the next whole number. E.g. floor(1.7) == 1, and floor(-2.7) == -2.
9861022
*

0 commit comments

Comments
 (0)