|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Takes an array of Game Objects, or any objects that have a public property as defined in `key`, |
| 9 | + * and then sets it to the given value. |
| 10 | + * |
| 11 | + * The optional `step` property is applied incrementally, multiplied by each item in the array. |
| 12 | + * |
| 13 | + * To use this with a Group: `PropertyValueSet(group.getChildren(), key, value, step)` |
| 14 | + * |
| 15 | + * @function Phaser.Actions.PropertyValueSet |
| 16 | + * @since 3.3.0 |
| 17 | + * |
| 18 | + * @param {array|Phaser.GameObjects.GameObject[]} items - The array of items to be updated by this action. |
| 19 | + * @param {string} key - The property to be updated. |
| 20 | + * @param {number} value - The amount to set the property to. |
| 21 | + * @param {number} [step=0] - This is added to the `value` amount, multiplied by the iteration counter. |
| 22 | + * @param {integer} [index=0] - An optional offset to start searching from within the items array. |
| 23 | + * @param {integer} [direction=1] - The direction to iterate through the array. 1 is from beginning to end, -1 from end to beginning. |
| 24 | + * |
| 25 | + * @return {array} The array of objects that were passed to this Action. |
| 26 | + */ |
| 27 | +var PropertyValueSet = function (items, key, value, step, index, direction) |
| 28 | +{ |
| 29 | + if (step === undefined) { step = 0; } |
| 30 | + if (index === undefined) { index = 0; } |
| 31 | + if (direction === undefined) { direction = 1; } |
| 32 | + |
| 33 | + var i; |
| 34 | + var t = 0; |
| 35 | + var end = items.length; |
| 36 | + |
| 37 | + if (direction === 1) |
| 38 | + { |
| 39 | + // Start to End |
| 40 | + for (i = index; i < end; i++) |
| 41 | + { |
| 42 | + items[i][key] = value + (t * step); |
| 43 | + t++; |
| 44 | + } |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + // End to Start |
| 49 | + for (i = index; i >= 0; i--) |
| 50 | + { |
| 51 | + items[i][key] = value + (t * step); |
| 52 | + t++; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return items; |
| 57 | +}; |
| 58 | + |
| 59 | +module.exports = PropertyValueSet; |
0 commit comments