Phaser.ArrayUtils = { getRandomItem: function (objects, startIndex, length){ if (objects == null ) { return null ; } if (typeof startIndex === 'undefined') { startIndex = 0; } if (typeof length === 'undefined') { length = _AN_Read_length('length', objects); } var randomIndex = startIndex + Math.floor(Math.random() * length); return objects[randomIndex] === undefined? null : objects[randomIndex]; } , removeRandomItem: function (objects, startIndex, length){ if (objects == null ) { return null ; } if (typeof startIndex === 'undefined') { startIndex = 0; } if (typeof length === 'undefined') { length = _AN_Read_length('length', objects); } var randomIndex = startIndex + Math.floor(Math.random() * length); if (randomIndex < _AN_Read_length('length', objects)) { var removed = objects.splice(randomIndex, 1); return removed[0] === undefined? null : removed[0]; } else { return null ; } } , shuffle: function (array){ for (var i = _AN_Read_length('length', array) - 1; i > 0; i-- ){ var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } , transposeMatrix: function (array){ var sourceRowCount = _AN_Read_length('length', array); var sourceColCount = _AN_Read_length('length', array[0]); var result = new Array(sourceColCount); for (var i = 0; i < sourceColCount; i++ ){ result[i] = new Array(sourceRowCount); for (var j = sourceRowCount - 1; j > -1; j-- ){ result[i][j] = array[j][i]; } } return result; } , rotateMatrix: function (matrix, direction){ if (typeof direction !== 'string') { direction = ((direction % 360) + 360) % 360; } if (direction === 90 || direction === -270 || direction === 'rotateLeft') { matrix = Phaser.ArrayUtils.transposeMatrix(matrix); matrix = matrix.reverse(); } else if (direction === -90 || direction === 270 || direction === 'rotateRight') { matrix = matrix.reverse(); matrix = Phaser.ArrayUtils.transposeMatrix(matrix); } else if (Math.abs(direction) === 180 || direction === 'rotate180') { for (var i = 0; i < _AN_Read_length('length', matrix); i++ ){ matrix[i].reverse(); } matrix = matrix.reverse(); } return matrix; } , findClosest: function (value, arr){ if (!_AN_Read_length('length', arr)) { return NaN; } else if (_AN_Read_length('length', arr) === 1 || value < arr[0]) { return arr[0]; } var i = 1; while (arr[i] < value){ i++ ; } var low = arr[i - 1]; var high = (i < _AN_Read_length('length', arr))? arr[i]: Number.POSITIVE_INFINITY; return ((high - value) <= (value - low))? high: low; } , rotate: function (array){ var s = array.shift(); array.push(s); return s; } , numberArray: function (start, end){ var result = [] ; for (var i = start; i <= end; i++ ){ result.push(i); } return result; } , numberArrayStep: function (start, end, step){ start = + start || 0; var type = typeof end; if ((type === 'number' || type === 'string') && step && step[end] === start) { end = step = null ; } step = step == null ? 1: (+ step || 0); if (end === null ) { end = start; start = 0; } else { end = + end || 0; } var index = -1; var length = Math.max(Phaser.Math.roundAwayFromZero((end - start) / (step || 1)), 0); var result = new Array(length); while (++index < length){ result[index] = start; start += step; } return result; } } ;