Skip to content

Commit d96382f

Browse files
committed
Added getFirstNth and getLastNth
1 parent 55965c7 commit d96382f

1 file changed

Lines changed: 76 additions & 11 deletions

File tree

src/gameobjects/group/Group.js

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ var Group = new Class({
686686
},
687687

688688
/**
689-
* Scans the group for the first member that has an {@link Phaser.GameObjects.GameObject#active} state matching the argument,
689+
* Scans the Group, from top to bottom, for the first member that has an {@link Phaser.GameObjects.GameObject#active} state matching the argument,
690690
* assigns `x` and `y`, and returns the member.
691691
*
692692
* If no matching member is found and `createIfNull` is true and the group isn't full then it will create a new Game Object using `x`, `y`, `key`, `frame`, and `visible`.
@@ -707,18 +707,44 @@ var Group = new Class({
707707
*/
708708
getFirst: function (state, createIfNull, x, y, key, frame, visible)
709709
{
710-
return this.getHandler(true, state, createIfNull, x, y, key, frame, visible);
710+
return this.getHandler(true, 1, state, createIfNull, x, y, key, frame, visible);
711711
},
712712

713713
/**
714-
* Scans the group for the last member that has an {@link Phaser.GameObjects.GameObject#active} state matching the argument,
714+
* Scans the Group, from top to bottom, for the nth member that has an {@link Phaser.GameObjects.GameObject#active} state matching the argument,
715+
* assigns `x` and `y`, and returns the member.
716+
*
717+
* If no matching member is found and `createIfNull` is true and the group isn't full then it will create a new Game Object using `x`, `y`, `key`, `frame`, and `visible`.
718+
* Unless a new member is created, `key`, `frame`, and `visible` are ignored.
719+
*
720+
* @method Phaser.GameObjects.Group#getFirstNth
721+
* @since 3.5.2
722+
*
723+
* @param {integer} nth - The nth matching Group member to search for.
724+
* @param {boolean} [state=false] - The {@link Phaser.GameObjects.GameObject#active} value to match.
725+
* @param {boolean} [createIfNull=false] - Create a new Game Object if no matching members are found, using the following arguments.
726+
* @param {number} [x] - The horizontal position of the Game Object in the world.
727+
* @param {number} [y] - The vertical position of the Game Object in the world.
728+
* @param {string} [key=defaultKey] - The texture key assigned to a new Game Object (if one is created).
729+
* @param {(string|integer)} [frame=defaultFrame] - A texture frame assigned to a new Game Object (if one is created).
730+
* @param {boolean} [visible=true] - The {@link Phaser.GameObjects.Components.Visible#visible} state of a new Game Object (if one is created).
731+
*
732+
* @return {?Phaser.GameObjects.GameObject} The first matching group member, or a newly created member, or null.
733+
*/
734+
getFirstNth: function (nth, state, createIfNull, x, y, key, frame, visible)
735+
{
736+
return this.getHandler(true, nth, state, createIfNull, x, y, key, frame, visible);
737+
},
738+
739+
/**
740+
* Scans the Group for the last member that has an {@link Phaser.GameObjects.GameObject#active} state matching the argument,
715741
* assigns `x` and `y`, and returns the member.
716742
*
717743
* If no matching member is found and `createIfNull` is true and the group isn't full then it will create a new Game Object using `x`, `y`, `key`, `frame`, and `visible`.
718744
* Unless a new member is created, `key`, `frame`, and `visible` are ignored.
719745
*
720746
* @method Phaser.GameObjects.Group#getLast
721-
* @since 3.6.0
747+
* @since 3.5.2
722748
*
723749
* @param {boolean} [state=false] - The {@link Phaser.GameObjects.GameObject#active} value to match.
724750
* @param {boolean} [createIfNull=false] - Create a new Game Object if no matching members are found, using the following arguments.
@@ -732,7 +758,33 @@ var Group = new Class({
732758
*/
733759
getLast: function (state, createIfNull, x, y, key, frame, visible)
734760
{
735-
return this.getHandler(false, state, createIfNull, x, y, key, frame, visible);
761+
return this.getHandler(false, 1, state, createIfNull, x, y, key, frame, visible);
762+
},
763+
764+
/**
765+
* Scans the Group for the last nth member that has an {@link Phaser.GameObjects.GameObject#active} state matching the argument,
766+
* assigns `x` and `y`, and returns the member.
767+
*
768+
* If no matching member is found and `createIfNull` is true and the group isn't full then it will create a new Game Object using `x`, `y`, `key`, `frame`, and `visible`.
769+
* Unless a new member is created, `key`, `frame`, and `visible` are ignored.
770+
*
771+
* @method Phaser.GameObjects.Group#getLastNth
772+
* @since 3.5.2
773+
*
774+
* @param {integer} nth - The nth matching Group member to search for.
775+
* @param {boolean} [state=false] - The {@link Phaser.GameObjects.GameObject#active} value to match.
776+
* @param {boolean} [createIfNull=false] - Create a new Game Object if no matching members are found, using the following arguments.
777+
* @param {number} [x] - The horizontal position of the Game Object in the world.
778+
* @param {number} [y] - The vertical position of the Game Object in the world.
779+
* @param {string} [key=defaultKey] - The texture key assigned to a new Game Object (if one is created).
780+
* @param {(string|integer)} [frame=defaultFrame] - A texture frame assigned to a new Game Object (if one is created).
781+
* @param {boolean} [visible=true] - The {@link Phaser.GameObjects.Components.Visible#visible} state of a new Game Object (if one is created).
782+
*
783+
* @return {?Phaser.GameObjects.GameObject} The first matching group member, or a newly created member, or null.
784+
*/
785+
getLastNth: function (nth, state, createIfNull, x, y, key, frame, visible)
786+
{
787+
return this.getHandler(false, nth, state, createIfNull, x, y, key, frame, visible);
736788
},
737789

738790
/**
@@ -744,9 +796,10 @@ var Group = new Class({
744796
*
745797
* @method Phaser.GameObjects.Group#getHandler
746798
* @private
747-
* @since 3.6.0
799+
* @since 3.5.2
748800
*
749801
* @param {boolean} forwards - Search front to back or back to front?
802+
* @param {integer} nth - Stop matching after nth successful matches.
750803
* @param {boolean} [state=false] - The {@link Phaser.GameObjects.GameObject#active} value to match.
751804
* @param {boolean} [createIfNull=false] - Create a new Game Object if no matching members are found, using the following arguments.
752805
* @param {number} [x] - The horizontal position of the Game Object in the world.
@@ -757,36 +810,48 @@ var Group = new Class({
757810
*
758811
* @return {?Phaser.GameObjects.GameObject} The first matching group member, or a newly created member, or null.
759812
*/
760-
getHandler: function (forwards, state, createIfNull, x, y, key, frame, visible)
813+
getHandler: function (forwards, nth, state, createIfNull, x, y, key, frame, visible)
761814
{
762815
if (state === undefined) { state = false; }
763816
if (createIfNull === undefined) { createIfNull = false; }
764817

765818
var gameObject;
766819

820+
var i;
821+
var total = 0;
767822
var children = this.children.entries;
768823

769824
if (forwards)
770825
{
771-
for (var i = 0; i < children.length; i++)
826+
for (i = 0; i < children.length; i++)
772827
{
773828
gameObject = children[i];
774829

775830
if (gameObject.active === state)
776831
{
777-
break;
832+
total++;
833+
834+
if (total === nth)
835+
{
836+
break;
837+
}
778838
}
779839
}
780840
}
781841
else
782842
{
783-
for (var i = children.length - 1; i >= 0; i--)
843+
for (i = children.length - 1; i >= 0; i--)
784844
{
785845
gameObject = children[i];
786846

787847
if (gameObject.active === state)
788848
{
789-
break;
849+
total++;
850+
851+
if (total === nth)
852+
{
853+
break;
854+
}
790855
}
791856
}
792857
}

0 commit comments

Comments
 (0)