Skip to content

Commit 24527ea

Browse files
committed
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 phaserjs#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 phaserjs#1013) Phaser.Utils.setProperty will set an Objects property regardless of depth (thanks @codevinsky phaserjs#1013) Phaser.Utils.setProperty will set an Objects property regardless of depth (thanks @codevinsky phaserjs#1013) Phaser.Utils.getProperty will get an Objects property regardless of depth (thanks @codevinsky phaserjs#1013)
1 parent 3da788b commit 24527ea

4 files changed

Lines changed: 42 additions & 29 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ Version 2.0.7 - "Amadicia" - -in development-
6767
* Animation.updateFrameData allows you to load a new FrameData object into an existing animation, even if currently running (based on #1029)
6868
* AnimationManager.loadFrameData will now update all existing Animations to use the newly loaded FrameData (based on #1029)
6969
* Sprite.loadTexture will store the `smoothed` property of the Sprite and re-apply it once the new texture is loaded.
70+
* 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)
71+
* 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)
72+
* Phaser.Utils.setProperty will set an Objects property regardless of depth (thanks @codevinsky #1013)
73+
* Phaser.Utils.setProperty will set an Objects property regardless of depth (thanks @codevinsky #1013)
74+
* Phaser.Utils.getProperty will get an Objects property regardless of depth (thanks @codevinsky #1013)
7075

7176
### Bug Fixes
7277

@@ -84,8 +89,7 @@ Version 2.0.7 - "Amadicia" - -in development-
8489
* Animation now guards against _frameData being null (thanks @lucbloom #1033)
8590
* Tilemap.swap now accurately swaps from A to B and from B to A (thanks @noidexe #1034)
8691
* BitmapData.resize fixed to update the crop property too, resolves issues with images getting cut off with BitmapData.load.
87-
88-
92+
* OrientationSprite fix as it's not using PIXI.TextureCache anymore (thanks @DarkDev- #1036)
8993

9094

9195
### Migration Guide
@@ -122,7 +126,7 @@ You can [clone the Phaser repo in Koding](https://koding.com/Teamwork?import=htt
122126

123127
If you use bower you can install phaser with:
124128

125-
`bower install phaser`
129+
`bower install phaser-official`
126130

127131
Nice and easy :)
128132

build/phaser.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,8 @@ declare module Phaser {
22822282
callAll(method: string, context: any, ...parameters: any[]): void;
22832283
callAllExists(callback: Function, existsValue: boolean, ...parameters: any[]): void;
22842284
callbackFromArray(child: Object, callback: Function, length: number): void;
2285+
checkAll(key: string[], value: any, checkAlive?: boolean, checkVisible?: boolean, force?: boolean): boolean;
2286+
checkProperty(child: Object, key: string[], value: any, force?: boolean): boolean;
22852287
countDead(): number;
22862288
countLiving(): number;
22872289
create(x: number, y: number, key: string, frame?: any, exists?: boolean): any;
@@ -5099,11 +5101,13 @@ declare module Phaser {
50995101
class Utils {
51005102

51015103
static extend(deep: boolean, target: Object): Object;
5104+
static getProperty(obj: Object, prop: string): any;
51025105
static isPlainObject(object: Object): boolean;
51035106
static mixin(from: Object, to: Object): Object;
51045107
static pad(str: string, len: number, pad: number, dir?: number): string;
51055108
static parseDimension(size: any, dimension: number): number;
51065109
static rotateArray<T>(array: T[], direction: any): T;
5110+
static setProperty(obj: Object, prop: string, value: any): Object;
51075111
static shuffle<T>(array: T[]): T;
51085112
static transposeArray<T>(array: T[]): T;
51095113

src/core/Group.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -760,24 +760,14 @@ Phaser.Group.prototype.checkProperty = function (child, key, value, force) {
760760

761761
if (typeof force === 'undefined') { force = false; }
762762

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-
772763
// 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.
774764
if (!Phaser.Utils.getProperty(child, key) && force)
775765
{
776766
return false;
777767
}
778-
779768

780-
if(Phaser.Utils.getProperty(child,key) !== value) {
769+
if (Phaser.Utils.getProperty(child, key) !== value)
770+
{
781771
return false;
782772
}
783773

@@ -891,10 +881,9 @@ Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkV
891881
};
892882

893883
/**
894-
* This function allows you to quickly check that the same property across all children of this Group is equal to the given value
884+
* This function allows you to quickly check that the same property across all children of this Group is equal to the given value.
895885
* 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.
896886
*
897-
*
898887
* @method Phaser.Group#checkAll
899888
* @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x'
900889
* @param {*} value - The value that will be checked.
@@ -903,6 +892,7 @@ Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkV
903892
* @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.
904893
*/
905894
Phaser.Group.prototype.checkAll = function (key, value, checkAlive, checkVisible, force) {
895+
906896
if (typeof checkAlive === 'undefined') { checkAlive = false; }
907897
if (typeof checkVisible === 'undefined') { checkVisible = false; }
908898
if (typeof force === 'undefined') { force = false; }
@@ -911,7 +901,8 @@ Phaser.Group.prototype.checkAll = function (key, value, checkAlive, checkVisible
911901
{
912902
if ((!checkAlive || (checkAlive && this.children[i].alive)) && (!checkVisible || (checkVisible && this.children[i].visible)))
913903
{
914-
if(!this.checkProperty(this.children[i], key, value, force)) {
904+
if (!this.checkProperty(this.children[i], key, value, force))
905+
{
915906
return false;
916907
}
917908
}

src/utils/Utils.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,69 @@
1313
Phaser.Utils = {
1414

1515
/**
16-
* Gets an object's property by string.
16+
* Gets an objects property by string.
1717
*
1818
* @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
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 .
2222
*/
2323
getProperty: function(obj, prop) {
24+
2425
var parts = prop.split('.'),
2526
last = parts.pop(),
2627
l = parts.length,
2728
i = 1,
2829
current = parts[0];
2930

30-
while(i < l && (obj = obj[current]) ) {
31+
while (i < l && (obj = obj[current]))
32+
{
3133
current = parts[i];
3234
i++;
3335
}
3436

35-
if(obj) {
37+
if (obj)
38+
{
3639
return obj[last];
37-
} else {
40+
}
41+
else
42+
{
3843
return null;
3944
}
45+
4046
},
47+
4148
/**
42-
* Sets an object's property by string.
49+
* Sets an objects property by string.
4350
*
4451
* @method Phaser.Utils.setProperty
4552
* @param {object} obj - The object to traverse
4653
* @param {string} prop - The property whose value will be changed
47-
54+
* @return {object} The object on which the property was set.
4855
*/
4956
setProperty: function(obj, prop, value) {
57+
5058
var parts = prop.split('.'),
5159
last = parts.pop(),
5260
l = parts.length,
5361
i = 1,
5462
current = parts[0];
5563

56-
while(i < l && (obj = obj[current]) ) {
64+
while (i < l && (obj = obj[current]))
65+
{
5766
current = parts[i];
5867
i++;
5968
}
6069

61-
if(obj) {
70+
if (obj)
71+
{
6272
obj[last] = value;
6373
}
74+
75+
return obj;
76+
6477
},
78+
6579
/**
6680
* Transposes the elements of the given Array.
6781
*

0 commit comments

Comments
 (0)