Skip to content

Commit 29dd09f

Browse files
committed
added: Phaser.Point.parse
static method that will take an object of any kind and return a new point based on the x & y properties of that object. optional x & y property names can also be supplied
1 parent 60888f6 commit 29dd09f

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/geom/Point.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,5 +864,26 @@ Phaser.Point.centroid = function (points, out) {
864864

865865
};
866866

867+
/**
868+
* Parses an object for x & y properties and returns a new Phaser.Point
869+
*
870+
* @method Phaser.Point#parse
871+
* @param {Object} obj - The object to parse
872+
* @param {string} [xProp='x'] - Optional x property name. Defaults to 'x'
873+
* @param {string} [yProp='y'] - Optional y property name. Defaults to 'y'
874+
* @return {Phaser.Point} The new Point object.
875+
* @static
876+
*/
877+
878+
Phaser.Point.parse = function(obj, xProp, yProp) {
879+
xProp = xProp || 'x';
880+
yProp = yProp || 'y';
881+
882+
if(obj.hasOwnProperty(xProp) && obj.hasOwnProperty(yProp)) {
883+
return new Phaser.Point(obj[xProp], obj[yProp]);
884+
}
885+
throw new Error('Object does not contain ' + xProp + ' and ' + yProp + ' properties.');
886+
};
887+
867888
// Because PIXI uses its own Point, we'll replace it with ours to avoid duplicating code or confusion.
868889
PIXI.Point = Phaser.Point;

0 commit comments

Comments
 (0)