Skip to content

Commit 5b4feed

Browse files
committed
Added Group.GetLast
1 parent 6772a54 commit 5b4feed

1 file changed

Lines changed: 81 additions & 9 deletions

File tree

src/gameobjects/group/Group.js

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,58 @@ var Group = new Class({
706706
* @return {?Phaser.GameObjects.GameObject} The first matching group member, or a newly created member, or null.
707707
*/
708708
getFirst: function (state, createIfNull, x, y, key, frame, visible)
709+
{
710+
return this.getHandler(true, state, createIfNull, x, y, key, frame, visible);
711+
},
712+
713+
/**
714+
* Scans the group for the last 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#getLast
721+
* @since 3.6.0
722+
*
723+
* @param {boolean} [state=false] - The {@link Phaser.GameObjects.GameObject#active} value to match.
724+
* @param {boolean} [createIfNull=false] - Create a new Game Object if no matching members are found, using the following arguments.
725+
* @param {number} [x] - The horizontal position of the Game Object in the world.
726+
* @param {number} [y] - The vertical position of the Game Object in the world.
727+
* @param {string} [key=defaultKey] - The texture key assigned to a new Game Object (if one is created).
728+
* @param {(string|integer)} [frame=defaultFrame] - A texture frame assigned to a new Game Object (if one is created).
729+
* @param {boolean} [visible=true] - The {@link Phaser.GameObjects.Components.Visible#visible} state of a new Game Object (if one is created).
730+
*
731+
* @return {?Phaser.GameObjects.GameObject} The first matching group member, or a newly created member, or null.
732+
*/
733+
getLast: function (state, createIfNull, x, y, key, frame, visible)
734+
{
735+
return this.getHandler(false, state, createIfNull, x, y, key, frame, visible);
736+
},
737+
738+
/**
739+
* Scans the group for the last member that has an {@link Phaser.GameObjects.GameObject#active} state matching the argument,
740+
* assigns `x` and `y`, and returns the member.
741+
*
742+
* 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`.
743+
* Unless a new member is created, `key`, `frame`, and `visible` are ignored.
744+
*
745+
* @method Phaser.GameObjects.Group#getHandler
746+
* @private
747+
* @since 3.6.0
748+
*
749+
* @param {boolean} forwards - Search front to back or back to front?
750+
* @param {boolean} [state=false] - The {@link Phaser.GameObjects.GameObject#active} value to match.
751+
* @param {boolean} [createIfNull=false] - Create a new Game Object if no matching members are found, using the following arguments.
752+
* @param {number} [x] - The horizontal position of the Game Object in the world.
753+
* @param {number} [y] - The vertical position of the Game Object in the world.
754+
* @param {string} [key=defaultKey] - The texture key assigned to a new Game Object (if one is created).
755+
* @param {(string|integer)} [frame=defaultFrame] - A texture frame assigned to a new Game Object (if one is created).
756+
* @param {boolean} [visible=true] - The {@link Phaser.GameObjects.Components.Visible#visible} state of a new Game Object (if one is created).
757+
*
758+
* @return {?Phaser.GameObjects.GameObject} The first matching group member, or a newly created member, or null.
759+
*/
760+
getHandler: function (forwards, state, createIfNull, x, y, key, frame, visible)
709761
{
710762
if (state === undefined) { state = false; }
711763
if (createIfNull === undefined) { createIfNull = false; }
@@ -714,24 +766,44 @@ var Group = new Class({
714766

715767
var children = this.children.entries;
716768

717-
for (var i = 0; i < children.length; i++)
769+
if (forwards)
718770
{
719-
gameObject = children[i];
720-
721-
if (gameObject.active === state)
771+
for (var i = 0; i < children.length; i++)
722772
{
723-
if (typeof(x) === 'number')
773+
gameObject = children[i];
774+
775+
if (gameObject.active === state)
724776
{
725-
gameObject.x = x;
777+
break;
726778
}
779+
}
780+
}
781+
else
782+
{
783+
for (var i = children.length - 1; i >= 0; i--)
784+
{
785+
gameObject = children[i];
727786

728-
if (typeof(y) === 'number')
787+
if (gameObject.active === state)
729788
{
730-
gameObject.y = y;
789+
break;
731790
}
791+
}
792+
}
732793

733-
return gameObject;
794+
if (gameObject)
795+
{
796+
if (typeof(x) === 'number')
797+
{
798+
gameObject.x = x;
734799
}
800+
801+
if (typeof(y) === 'number')
802+
{
803+
gameObject.y = y;
804+
}
805+
806+
return gameObject;
735807
}
736808

737809
// Got this far? We need to create or bail

0 commit comments

Comments
 (0)