Group.checkAll allows you to check if the same property exists across all children of the Group and is set to the given value (thanks @codevinsky #1013)

Group.checkProperty allows you to check if the property exists on the given child of the Group and is set to the value specified (thanks @codevinsky #1013)
Phaser.Utils.setProperty will set an Objects property regardless of depth (thanks @codevinsky #1013)
Phaser.Utils.setProperty will set an Objects property regardless of depth (thanks @codevinsky #1013)
Phaser.Utils.getProperty will get an Objects property regardless of depth (thanks @codevinsky #1013)
This commit is contained in:
photonstorm 2014-07-18 11:52:39 +01:00
parent 3da788bd1d
commit 24527eac3e
4 changed files with 42 additions and 29 deletions

View file

@ -67,6 +67,11 @@ Version 2.0.7 - "Amadicia" - -in development-
* Animation.updateFrameData allows you to load a new FrameData object into an existing animation, even if currently running (based on #1029)
* AnimationManager.loadFrameData will now update all existing Animations to use the newly loaded FrameData (based on #1029)
* Sprite.loadTexture will store the `smoothed` property of the Sprite and re-apply it once the new texture is loaded.
* Group.checkAll allows you to check if the same property exists across all children of the Group and is set to the given value (thanks @codevinsky #1013)
* Group.checkProperty allows you to check if the property exists on the given child of the Group and is set to the value specified (thanks @codevinsky #1013)
* Phaser.Utils.setProperty will set an Objects property regardless of depth (thanks @codevinsky #1013)
* Phaser.Utils.setProperty will set an Objects property regardless of depth (thanks @codevinsky #1013)
* Phaser.Utils.getProperty will get an Objects property regardless of depth (thanks @codevinsky #1013)
### Bug Fixes
@ -84,8 +89,7 @@ Version 2.0.7 - "Amadicia" - -in development-
* Animation now guards against _frameData being null (thanks @lucbloom #1033)
* Tilemap.swap now accurately swaps from A to B and from B to A (thanks @noidexe #1034)
* BitmapData.resize fixed to update the crop property too, resolves issues with images getting cut off with BitmapData.load.
* OrientationSprite fix as it's not using PIXI.TextureCache anymore (thanks @DarkDev- #1036)
### Migration Guide
@ -122,7 +126,7 @@ You can [clone the Phaser repo in Koding](https://koding.com/Teamwork?import=htt
If you use bower you can install phaser with:
`bower install phaser`
`bower install phaser-official`
Nice and easy :)

4
build/phaser.d.ts vendored
View file

@ -2282,6 +2282,8 @@ declare module Phaser {
callAll(method: string, context: any, ...parameters: any[]): void;
callAllExists(callback: Function, existsValue: boolean, ...parameters: any[]): void;
callbackFromArray(child: Object, callback: Function, length: number): void;
checkAll(key: string[], value: any, checkAlive?: boolean, checkVisible?: boolean, force?: boolean): boolean;
checkProperty(child: Object, key: string[], value: any, force?: boolean): boolean;
countDead(): number;
countLiving(): number;
create(x: number, y: number, key: string, frame?: any, exists?: boolean): any;
@ -5099,11 +5101,13 @@ declare module Phaser {
class Utils {
static extend(deep: boolean, target: Object): Object;
static getProperty(obj: Object, prop: string): any;
static isPlainObject(object: Object): boolean;
static mixin(from: Object, to: Object): Object;
static pad(str: string, len: number, pad: number, dir?: number): string;
static parseDimension(size: any, dimension: number): number;
static rotateArray<T>(array: T[], direction: any): T;
static setProperty(obj: Object, prop: string, value: any): Object;
static shuffle<T>(array: T[]): T;
static transposeArray<T>(array: T[]): T;

View file

@ -760,24 +760,14 @@ 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) {
if (Phaser.Utils.getProperty(child, key) !== value)
{
return false;
}
@ -891,10 +881,9 @@ 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 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.
@ -903,6 +892,7 @@ Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkV
* @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; }
@ -911,7 +901,8 @@ Phaser.Group.prototype.checkAll = function (key, value, checkAlive, checkVisible
{
if ((!checkAlive || (checkAlive && this.children[i].alive)) && (!checkVisible || (checkVisible && this.children[i].visible)))
{
if(!this.checkProperty(this.children[i], key, value, force)) {
if (!this.checkProperty(this.children[i], key, value, force))
{
return false;
}
}

View file

@ -13,55 +13,69 @@
Phaser.Utils = {
/**
* Gets an object's property by string.
* Gets an objects 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
* @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]) ) {
while (i < l && (obj = obj[current]))
{
current = parts[i];
i++;
}
if(obj) {
if (obj)
{
return obj[last];
} else {
}
else
{
return null;
}
},
/**
* Sets an object's property by string.
* Sets an objects property by string.
*
* @method Phaser.Utils.setProperty
* @param {object} obj - The object to traverse
* @param {string} prop - The property whose value will be changed
* @return {object} The object on which the property was set.
*/
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]) ) {
while (i < l && (obj = obj[current]))
{
current = parts[i];
i++;
}
if(obj) {
if (obj)
{
obj[last] = value;
}
return obj;
},
/**
* Transposes the elements of the given Array.
*