Skip to content

Commit 1f24d95

Browse files
committed
New force parameter added to Group.set, setAll, setAllChildren, setProperty which controls if a property is created even if it doesn't exist.
Group.hasProperty will check a child for the given property and return a boolean.
1 parent 422e769 commit 1f24d95

1 file changed

Lines changed: 73 additions & 15 deletions

File tree

src/core/Group.js

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -632,15 +632,57 @@ Phaser.Group.prototype.replace = function (oldChild, newChild) {
632632
};
633633

634634
/**
635-
* Sets the given property to the given value on the child. The operation controls the assignment of the value.
635+
* Checks if the child has the given property. Will scan up to 4 levels deep only.
636+
*
637+
* @method Phaser.Group#hasProperty
638+
* @param {*} child - The child to check for the existance of the property on.
639+
* @param {array} key - An array of strings that make up the property.
640+
* @return {boolean} True if the child has the property, otherwise false.
641+
*/
642+
Phaser.Group.prototype.hasProperty = function (child, key) {
643+
644+
var len = key.length;
645+
646+
if (len === 1 && child.hasOwnProperty(key[0]))
647+
{
648+
return true;
649+
}
650+
else if (len === 2 && child.hasOwnProperty(key[0]) && child[key[0]].hasOwnProperty(key[1]))
651+
{
652+
return true;
653+
}
654+
else if (len === 3 && child.hasOwnProperty(key[0]) && child[key[0]].hasOwnProperty(key[1]) && child[key[0]][key[1]].hasOwnProperty(key[2]))
655+
{
656+
return true;
657+
}
658+
else if (len === 4 && child.hasOwnProperty(key[0]) && child[key[0]].hasOwnProperty(key[1]) && child[key[0]][key[1]].hasOwnProperty(key[2]) && child[key[0]][key[1]][key[2]].hasOwnProperty(key[3]))
659+
{
660+
return true;
661+
}
662+
663+
return false;
664+
665+
};
666+
667+
/**
668+
* Sets a property to the given value on the child. The operation parameter controls how the value is set.
669+
* Operation 0 means set the existing value to the given value, or if force is `false` create a new property with the given value.
670+
* 1 will add the given value to the value already present.
671+
* 2 will subtract the given value from the value already present.
672+
* 3 will multiply the value already present by the given value.
673+
* 4 will divide the value already present by the given value.
636674
*
637675
* @method Phaser.Group#setProperty
638676
* @param {*} child - The child to set the property value on.
639677
* @param {array} key - An array of strings that make up the property that will be set.
640678
* @param {*} value - The value that will be set.
641679
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
680+
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
681+
* @return {boolean} True if the property was set, false if not.
642682
*/
643-
Phaser.Group.prototype.setProperty = function (child, key, value, operation) {
683+
Phaser.Group.prototype.setProperty = function (child, key, value, operation, force) {
684+
685+
if (typeof force === 'undefined') { force = false; }
644686

645687
operation = operation || 0;
646688

@@ -652,33 +694,40 @@ Phaser.Group.prototype.setProperty = function (child, key, value, operation) {
652694
// 3 = Multiply
653695
// 4 = Divide
654696

697+
// We can't force a property in and the child doesn't have it, so abort.
698+
// Equally we can't add, subtract, multiply or divide a property value if it doesn't exist, so abort in those cases too.
699+
if (!this.hasProperty(child, key, value) && (!force || operation > 0))
700+
{
701+
return false;
702+
}
703+
655704
var len = key.length;
656705

657-
if (len === 1 && child.hasOwnProperty(key[0]))
706+
if (len === 1)
658707
{
659708
if (operation === 0) { child[key[0]] = value; }
660709
else if (operation == 1) { child[key[0]] += value; }
661710
else if (operation == 2) { child[key[0]] -= value; }
662711
else if (operation == 3) { child[key[0]] *= value; }
663712
else if (operation == 4) { child[key[0]] /= value; }
664713
}
665-
else if (len === 2 && child.hasOwnProperty(key[0]) && child[key[0]].hasOwnProperty(key[1]))
714+
else if (len === 2)
666715
{
667716
if (operation === 0) { child[key[0]][key[1]] = value; }
668717
else if (operation == 1) { child[key[0]][key[1]] += value; }
669718
else if (operation == 2) { child[key[0]][key[1]] -= value; }
670719
else if (operation == 3) { child[key[0]][key[1]] *= value; }
671720
else if (operation == 4) { child[key[0]][key[1]] /= value; }
672721
}
673-
else if (len === 3 && child.hasOwnProperty(key[0]) && child[key[0]].hasOwnProperty(key[1]) && child[key[0]][key[1]].hasOwnProperty(key[2]))
722+
else if (len === 3)
674723
{
675724
if (operation === 0) { child[key[0]][key[1]][key[2]] = value; }
676725
else if (operation == 1) { child[key[0]][key[1]][key[2]] += value; }
677726
else if (operation == 2) { child[key[0]][key[1]][key[2]] -= value; }
678727
else if (operation == 3) { child[key[0]][key[1]][key[2]] *= value; }
679728
else if (operation == 4) { child[key[0]][key[1]][key[2]] /= value; }
680729
}
681-
else if (len === 4 && child.hasOwnProperty(key[0]) && child[key[0]].hasOwnProperty(key[1]) && child[key[0]][key[1]].hasOwnProperty(key[2]) && child[key[0]][key[1]][key[2]].hasOwnProperty(key[3]))
730+
else if (len === 4)
682731
{
683732
if (operation === 0) { child[key[0]][key[1]][key[2]][key[3]] = value; }
684733
else if (operation == 1) { child[key[0]][key[1]][key[2]][key[3]] += value; }
@@ -687,6 +736,8 @@ Phaser.Group.prototype.setProperty = function (child, key, value, operation) {
687736
else if (operation == 4) { child[key[0]][key[1]][key[2]][key[3]] /= value; }
688737
}
689738

739+
return true;
740+
690741
};
691742

692743
/**
@@ -700,8 +751,12 @@ Phaser.Group.prototype.setProperty = function (child, key, value, operation) {
700751
* @param {boolean} [checkAlive=false] - If set then the child will only be updated if alive=true.
701752
* @param {boolean} [checkVisible=false] - If set then the child will only be updated if visible=true.
702753
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
754+
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
755+
* @return {boolean} True if the property was set, false if not.
703756
*/
704-
Phaser.Group.prototype.set = function (child, key, value, checkAlive, checkVisible, operation) {
757+
Phaser.Group.prototype.set = function (child, key, value, checkAlive, checkVisible, operation, force) {
758+
759+
if (typeof force === 'undefined') { force = false; }
705760

706761
key = key.split('.');
707762

@@ -710,7 +765,7 @@ Phaser.Group.prototype.set = function (child, key, value, checkAlive, checkVisib
710765

711766
if ((checkAlive === false || (checkAlive && child.alive)) && (checkVisible === false || (checkVisible && child.visible)))
712767
{
713-
this.setProperty(child, key, value, operation);
768+
return this.setProperty(child, key, value, operation, force);
714769
}
715770

716771
};
@@ -728,21 +783,22 @@ Phaser.Group.prototype.set = function (child, key, value, checkAlive, checkVisib
728783
* @param {boolean} [checkAlive=false] - If set then only children with alive=true will be updated. This includes any Groups that are children.
729784
* @param {boolean} [checkVisible=false] - If set then only children with visible=true will be updated. This includes any Groups that are children.
730785
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
786+
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
731787
*/
732-
Phaser.Group.prototype.setAll = function (key, value, checkAlive, checkVisible, operation) {
733-
734-
key = key.split('.');
788+
Phaser.Group.prototype.setAll = function (key, value, checkAlive, checkVisible, operation, force) {
735789

736790
if (typeof checkAlive === 'undefined') { checkAlive = false; }
737791
if (typeof checkVisible === 'undefined') { checkVisible = false; }
792+
if (typeof force === 'undefined') { force = false; }
738793

794+
key = key.split('.');
739795
operation = operation || 0;
740796

741797
for (var i = 0, len = this.children.length; i < len; i++)
742798
{
743799
if ((!checkAlive || (checkAlive && this.children[i].alive)) && (!checkVisible || (checkVisible && this.children[i].visible)))
744800
{
745-
this.setProperty(this.children[i], key, value, operation);
801+
this.setProperty(this.children[i], key, value, operation, force);
746802
}
747803
}
748804

@@ -762,11 +818,13 @@ Phaser.Group.prototype.setAll = function (key, value, checkAlive, checkVisible,
762818
* @param {boolean} [checkAlive=false] - If set then only children with alive=true will be updated. This includes any Groups that are children.
763819
* @param {boolean} [checkVisible=false] - If set then only children with visible=true will be updated. This includes any Groups that are children.
764820
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
821+
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
765822
*/
766-
Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkVisible, operation) {
823+
Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkVisible, operation, force) {
767824

768825
if (typeof checkAlive === 'undefined') { checkAlive = false; }
769826
if (typeof checkVisible === 'undefined') { checkVisible = false; }
827+
if (typeof force === 'undefined') { force = false; }
770828

771829
operation = operation || 0;
772830

@@ -776,11 +834,11 @@ Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkV
776834
{
777835
if (this.children[i] instanceof Phaser.Group)
778836
{
779-
this.children[i].setAllChildren(key, value, checkAlive, checkVisible, operation);
837+
this.children[i].setAllChildren(key, value, checkAlive, checkVisible, operation, force);
780838
}
781839
else
782840
{
783-
this.setProperty(this.children[i], key.split('.'), value, operation);
841+
this.setProperty(this.children[i], key.split('.'), value, operation, force);
784842
}
785843
}
786844
}

0 commit comments

Comments
 (0)