Skip to content

Commit 3da788b

Browse files
committed
Merge pull request phaserjs#1013 from codevinsky/group-checkall
added: Phaser.Utils.getProperty / Phaser.Utils.setProperty; added: Phase...
2 parents 491d7c2 + aad74ff commit 3da788b

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

src/core/Group.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,45 @@ Phaser.Group.prototype.setProperty = function (child, key, value, operation, for
746746

747747
};
748748

749+
/**
750+
* Checks a property for the given value on the child.
751+
*
752+
* @method Phaser.Group#checkProperty
753+
* @param {*} child - The child to check the property value on.
754+
* @param {array} key - An array of strings that make up the property that will be set.
755+
* @param {*} value - The value that will be checked.
756+
* @param {boolean} [force=false] - If `force` is true then the property will be checked on the child regardless if it already exists or not. If true and the property doesn't exist, false will be returned.
757+
* @return {boolean} True if the property was was equal to value, false if not.
758+
*/
759+
Phaser.Group.prototype.checkProperty = function (child, key, value, force) {
760+
761+
if (typeof force === 'undefined') { force = false; }
762+
763+
764+
// As ugly as this approach looks, and although it's limited to a depth of only 4, it's much faster than a for loop or object iteration.
765+
766+
// 0 = Equals
767+
// 1 = Add
768+
// 2 = Subtract
769+
// 3 = Multiply
770+
// 4 = Divide
771+
772+
// We can't force a property in and the child doesn't have it, so abort.
773+
// Equally we can't add, subtract, multiply or divide a property value if it doesn't exist, so abort in those cases too.
774+
if (!Phaser.Utils.getProperty(child, key) && force)
775+
{
776+
return false;
777+
}
778+
779+
780+
if(Phaser.Utils.getProperty(child,key) !== value) {
781+
return false;
782+
}
783+
784+
return true;
785+
786+
};
787+
749788
/**
750789
* This function allows you to quickly set a property on a single child of this Group to a new value.
751790
* The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.
@@ -851,6 +890,37 @@ Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkV
851890

852891
};
853892

893+
/**
894+
* This function allows you to quickly check that the same property across all children of this Group is equal to the given value
895+
* This call doesn't descend down children, so if you have a Group inside of this Group, the property will be checked on the Group but not its children.
896+
*
897+
*
898+
* @method Phaser.Group#checkAll
899+
* @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x'
900+
* @param {*} value - The value that will be checked.
901+
* @param {boolean} [checkAlive=false] - If set then only children with alive=true will be checked. This includes any Groups that are children.
902+
* @param {boolean} [checkVisible=false] - If set then only children with visible=true will be checked. This includes any Groups that are children.
903+
* @param {boolean} [force=false] - If `force` is true then the property will be checked on the child regardless if it already exists or not. If true and the property doesn't exist, false will be returned.
904+
*/
905+
Phaser.Group.prototype.checkAll = function (key, value, checkAlive, checkVisible, force) {
906+
if (typeof checkAlive === 'undefined') { checkAlive = false; }
907+
if (typeof checkVisible === 'undefined') { checkVisible = false; }
908+
if (typeof force === 'undefined') { force = false; }
909+
910+
for (var i = 0, len = this.children.length; i < len; i++)
911+
{
912+
if ((!checkAlive || (checkAlive && this.children[i].alive)) && (!checkVisible || (checkVisible && this.children[i].visible)))
913+
{
914+
if(!this.checkProperty(this.children[i], key, value, force)) {
915+
return false;
916+
}
917+
}
918+
}
919+
920+
return true;
921+
922+
};
923+
854924
/**
855925
* Adds the amount to the given property on all children in this Group.
856926
* Group.addAll('x', 10) will add 10 to the child.x value.

src/utils/Utils.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,56 @@
1212
*/
1313
Phaser.Utils = {
1414

15+
/**
16+
* Gets an object's property by string.
17+
*
18+
* @method Phaser.Utils.getProperty
19+
* @param {object} obj - The object to traverse
20+
* @param {string} prop - The property whose value will be returned
21+
* @return {*} the value of the property or null if property isn't found
22+
*/
23+
getProperty: function(obj, prop) {
24+
var parts = prop.split('.'),
25+
last = parts.pop(),
26+
l = parts.length,
27+
i = 1,
28+
current = parts[0];
29+
30+
while(i < l && (obj = obj[current]) ) {
31+
current = parts[i];
32+
i++;
33+
}
34+
35+
if(obj) {
36+
return obj[last];
37+
} else {
38+
return null;
39+
}
40+
},
41+
/**
42+
* Sets an object's property by string.
43+
*
44+
* @method Phaser.Utils.setProperty
45+
* @param {object} obj - The object to traverse
46+
* @param {string} prop - The property whose value will be changed
47+
48+
*/
49+
setProperty: function(obj, prop, value) {
50+
var parts = prop.split('.'),
51+
last = parts.pop(),
52+
l = parts.length,
53+
i = 1,
54+
current = parts[0];
55+
56+
while(i < l && (obj = obj[current]) ) {
57+
current = parts[i];
58+
i++;
59+
}
60+
61+
if(obj) {
62+
obj[last] = value;
63+
}
64+
},
1565
/**
1666
* Transposes the elements of the given Array.
1767
*

0 commit comments

Comments
 (0)