2018-03-17 17:16:11 +00:00
|
|
|
/**
|
2024-02-19 17:12:18 +00:00
|
|
|
* @author Richard Davey <rich@phaser.io>
|
|
|
|
* @copyright 2013-2024 Phaser Studio Inc.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-03-17 17:16:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes an array of objects and returns the last element in the array that has properties which match
|
|
|
|
* all of those specified in the `compare` object. For example, if the compare object was: `{ scaleX: 0.5, alpha: 1 }`
|
|
|
|
* then it would return the last item which had the property `scaleX` set to 0.5 and `alpha` set to 1.
|
2018-03-20 14:57:12 +00:00
|
|
|
*
|
2018-03-17 17:16:11 +00:00
|
|
|
* To use this with a Group: `GetLast(group.getChildren(), compare, index)`
|
|
|
|
*
|
|
|
|
* @function Phaser.Actions.GetLast
|
|
|
|
* @since 3.3.0
|
2018-03-20 14:57:12 +00:00
|
|
|
*
|
2018-03-27 11:14:08 +00:00
|
|
|
* @generic {Phaser.GameObjects.GameObject[]} G - [items]
|
|
|
|
*
|
2018-03-20 14:57:12 +00:00
|
|
|
* @param {(array|Phaser.GameObjects.GameObject[])} items - The array of items to be searched by this action.
|
2018-03-17 17:16:11 +00:00
|
|
|
* @param {object} compare - The comparison object. Each property in this object will be checked against the items of the array.
|
2020-11-23 10:22:13 +00:00
|
|
|
* @param {number} [index=0] - An optional offset to start searching from within the items array.
|
2018-03-20 14:57:12 +00:00
|
|
|
*
|
2018-03-20 22:36:41 +00:00
|
|
|
* @return {?(object|Phaser.GameObjects.GameObject)} The last object in the array that matches the comparison object, or `null` if no match was found.
|
2018-03-17 17:16:11 +00:00
|
|
|
*/
|
|
|
|
var GetLast = function (items, compare, index)
|
|
|
|
{
|
|
|
|
if (index === undefined) { index = 0; }
|
|
|
|
|
2023-05-28 23:21:36 +00:00
|
|
|
for (var i = items.length - 1; i >= index; i--)
|
2018-03-17 17:16:11 +00:00
|
|
|
{
|
|
|
|
var item = items[i];
|
|
|
|
|
|
|
|
var match = true;
|
|
|
|
|
|
|
|
for (var property in compare)
|
|
|
|
{
|
|
|
|
if (item[property] !== compare[property])
|
|
|
|
{
|
|
|
|
match = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match)
|
|
|
|
{
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GetLast;
|