Skip to content

Commit 684838a

Browse files
committed
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.
1 parent ef3f6c0 commit 684838a

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* 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)
1010
* `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)
11+
* `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.
1112

1213
### Bug Fixes
1314

src/gameobjects/shape/polygon/Polygon.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var Smooth = require('../../../geom/polygon/Smooth');
2424
* The Polygon Shape is created by providing a list of points, which are then used to create an
2525
* internal Polygon geometry object. The points can be set from a variety of formats:
2626
*
27+
* - A string containing paired values separated by a single space: `'40 0 40 20 100 20 100 80 40 80 40 100 0 50'`
2728
* - An array of Point or Vector2 objects: `[new Phaser.Math.Vec2(x1, y1), ...]`
2829
* - An array of objects with public x/y properties: `[obj1, obj2, ...]`
2930
* - An array of paired numbers that represent point coordinates: `[x1,y1, x2,y2, ...]`

src/geom/polygon/Polygon.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var Polygon = new Class({
7171
*
7272
* The points can be set from a variety of formats:
7373
*
74+
* - A string containing paired values separated by a single space: `'40 0 40 20 100 20 100 80 40 80 40 100 0 50'`
7475
* - An array of Point objects: `[new Phaser.Point(x1, y1), ...]`
7576
* - An array of objects with public x/y properties: `[obj1, obj2, ...]`
7677
* - An array of paired numbers that represent point coordinates: `[x1,y1, x2,y2, ...]`
@@ -90,6 +91,11 @@ var Polygon = new Class({
9091
this.area = 0;
9192
this.points = [];
9293

94+
if (typeof points === 'string')
95+
{
96+
points = points.split(' ');
97+
}
98+
9399
if (!Array.isArray(points))
94100
{
95101
return this;
@@ -103,10 +109,10 @@ var Polygon = new Class({
103109
{
104110
p = { x: 0, y: 0 };
105111

106-
if (typeof points[i] === 'number')
112+
if (typeof points[i] === 'number' || typeof points[i] === 'string')
107113
{
108-
p.x = points[i];
109-
p.y = points[i + 1];
114+
p.x = parseFloat(points[i]);
115+
p.y = parseFloat(points[i + 1]);
110116
i++;
111117
}
112118
else if (Array.isArray(points[i]))

0 commit comments

Comments
 (0)