diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a8a91682..423a34aca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * The Loader has been updated to handle the impact of you destroying the game instance while still processing files. It will no longer throw cache and texture related errors. Fix #4049 (thanks @pantoninho) * `TileSet.getTileData()` has been updated so it will return tile data from either Tiled 1.1.x or the new Tiled 1.2.x JSON structure. Fix #3998 (thanks @martin-pabst @halgorithm) +* `Polygon.setTo` can now take a string of space separated numbers when creating the polygon data, i.e.: `'40 0 40 20 100 20 100 80 40 80 40 100 0 50'`. This update also impacts the Polygon Shape object, which can now also take this format as well. ### Bug Fixes diff --git a/src/gameobjects/shape/polygon/Polygon.js b/src/gameobjects/shape/polygon/Polygon.js index 4feb77454..629bcdb30 100644 --- a/src/gameobjects/shape/polygon/Polygon.js +++ b/src/gameobjects/shape/polygon/Polygon.js @@ -24,6 +24,7 @@ var Smooth = require('../../../geom/polygon/Smooth'); * The Polygon Shape is created by providing a list of points, which are then used to create an * internal Polygon geometry object. The points can be set from a variety of formats: * + * - A string containing paired values separated by a single space: `'40 0 40 20 100 20 100 80 40 80 40 100 0 50'` * - An array of Point or Vector2 objects: `[new Phaser.Math.Vec2(x1, y1), ...]` * - An array of objects with public x/y properties: `[obj1, obj2, ...]` * - An array of paired numbers that represent point coordinates: `[x1,y1, x2,y2, ...]` diff --git a/src/geom/polygon/Polygon.js b/src/geom/polygon/Polygon.js index fb60b3522..4689b73fa 100644 --- a/src/geom/polygon/Polygon.js +++ b/src/geom/polygon/Polygon.js @@ -71,6 +71,7 @@ var Polygon = new Class({ * * The points can be set from a variety of formats: * + * - A string containing paired values separated by a single space: `'40 0 40 20 100 20 100 80 40 80 40 100 0 50'` * - An array of Point objects: `[new Phaser.Point(x1, y1), ...]` * - An array of objects with public x/y properties: `[obj1, obj2, ...]` * - An array of paired numbers that represent point coordinates: `[x1,y1, x2,y2, ...]` @@ -90,6 +91,11 @@ var Polygon = new Class({ this.area = 0; this.points = []; + if (typeof points === 'string') + { + points = points.split(' '); + } + if (!Array.isArray(points)) { return this; @@ -103,10 +109,10 @@ var Polygon = new Class({ { p = { x: 0, y: 0 }; - if (typeof points[i] === 'number') + if (typeof points[i] === 'number' || typeof points[i] === 'string') { - p.x = points[i]; - p.y = points[i + 1]; + p.x = parseFloat(points[i]); + p.y = parseFloat(points[i + 1]); i++; } else if (Array.isArray(points[i]))