2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2016-12-07 13:34:21 +00:00
|
|
|
var SpliceOne = require('./SpliceOne');
|
|
|
|
|
2017-10-12 12:57:55 +00:00
|
|
|
/**
|
2018-01-26 14:23:00 +00:00
|
|
|
* Removes a random object from the given array and returns it.
|
|
|
|
* Will return null if there are no array items that fall within the specified range or if there is no item for the randomly chosen index.
|
2017-10-12 12:57:55 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Utils.Array.RemoveRandomElement
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {array} array - The array to removed a random element from.
|
|
|
|
* @param {integer} [start=0] - The array index to start the search from.
|
|
|
|
* @param {integer} [length=array.length] - Optional restriction on the number of elements to randomly select from.
|
|
|
|
*
|
|
|
|
* @return {object} The random element that was removed, or `null` if there were no array elements that fell within the given range.
|
|
|
|
*/
|
2016-12-07 13:34:21 +00:00
|
|
|
var RemoveRandomElement = function (array, start, length)
|
|
|
|
{
|
|
|
|
if (start === undefined) { start = 0; }
|
|
|
|
if (length === undefined) { length = array.length; }
|
|
|
|
|
|
|
|
var randomIndex = start + Math.floor(Math.random() * length);
|
|
|
|
|
|
|
|
return SpliceOne(array, randomIndex);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RemoveRandomElement;
|