Merge pull request #1013 from codevinsky/group-checkall

added: Phaser.Utils.getProperty / Phaser.Utils.setProperty; added: Phase...
This commit is contained in:
Richard Davey 2014-07-18 11:37:38 +01:00
commit 3da788bd1d
2 changed files with 120 additions and 0 deletions

View file

@ -746,6 +746,45 @@ Phaser.Group.prototype.setProperty = function (child, key, value, operation, for
};
/**
* Checks a property for the given value on the child.
*
* @method Phaser.Group#checkProperty
* @param {*} child - The child to check the property value on.
* @param {array} key - An array of strings that make up the property that will be set.
* @param {*} value - The value that will be checked.
* @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.
* @return {boolean} True if the property was was equal to value, false if not.
*/
Phaser.Group.prototype.checkProperty = function (child, key, value, force) {
if (typeof force === 'undefined') { force = false; }
// 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.
// 0 = Equals
// 1 = Add
// 2 = Subtract
// 3 = Multiply
// 4 = Divide
// We can't force a property in and the child doesn't have it, so abort.
// Equally we can't add, subtract, multiply or divide a property value if it doesn't exist, so abort in those cases too.
if (!Phaser.Utils.getProperty(child, key) && force)
{
return false;
}
if(Phaser.Utils.getProperty(child,key) !== value) {
return false;
}
return true;
};
/**
* This function allows you to quickly set a property on a single child of this Group to a new value.
* 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
};
/**
* This function allows you to quickly check that the same property across all children of this Group is equal to the given value
* 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.
*
*
* @method Phaser.Group#checkAll
* @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x'
* @param {*} value - The value that will be checked.
* @param {boolean} [checkAlive=false] - If set then only children with alive=true will be checked. This includes any Groups that are children.
* @param {boolean} [checkVisible=false] - If set then only children with visible=true will be checked. This includes any Groups that are children.
* @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.
*/
Phaser.Group.prototype.checkAll = function (key, value, checkAlive, checkVisible, force) {
if (typeof checkAlive === 'undefined') { checkAlive = false; }
if (typeof checkVisible === 'undefined') { checkVisible = false; }
if (typeof force === 'undefined') { force = false; }
for (var i = 0, len = this.children.length; i < len; i++)
{
if ((!checkAlive || (checkAlive && this.children[i].alive)) && (!checkVisible || (checkVisible && this.children[i].visible)))
{
if(!this.checkProperty(this.children[i], key, value, force)) {
return false;
}
}
}
return true;
};
/**
* Adds the amount to the given property on all children in this Group.
* Group.addAll('x', 10) will add 10 to the child.x value.

View file

@ -12,6 +12,56 @@
*/
Phaser.Utils = {
/**
* Gets an object's property by string.
*
* @method Phaser.Utils.getProperty
* @param {object} obj - The object to traverse
* @param {string} prop - The property whose value will be returned
* @return {*} the value of the property or null if property isn't found
*/
getProperty: function(obj, prop) {
var parts = prop.split('.'),
last = parts.pop(),
l = parts.length,
i = 1,
current = parts[0];
while(i < l && (obj = obj[current]) ) {
current = parts[i];
i++;
}
if(obj) {
return obj[last];
} else {
return null;
}
},
/**
* Sets an object's property by string.
*
* @method Phaser.Utils.setProperty
* @param {object} obj - The object to traverse
* @param {string} prop - The property whose value will be changed
*/
setProperty: function(obj, prop, value) {
var parts = prop.split('.'),
last = parts.pop(),
l = parts.length,
i = 1,
current = parts[0];
while(i < l && (obj = obj[current]) ) {
current = parts[i];
i++;
}
if(obj) {
obj[last] = value;
}
},
/**
* Transposes the elements of the given Array.
*