Skip to content

Commit 819826c

Browse files
committed
Added Polygon.flatten and Polygon.toNumberArray.
Fixed Graphics.lineTo if no moveTo has been specified. Fixed Graphics.drawShape if a mixed-type Polygon has been given.
1 parent 97ebfce commit 819826c

3 files changed

Lines changed: 168 additions & 126 deletions

File tree

src/geom/Polygon.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,50 @@ Phaser.Polygon = function () {
5353

5454
Phaser.Polygon.prototype = {
5555

56+
/**
57+
* Export the points as an array of flat numbers, following the sequence [ x,y, x,y, x,y ]
58+
*
59+
* @method Phaser.Polygon#toNumberArray
60+
* @param {array} [output] - The array to append the points to. If not specified a new array will be created.
61+
* @return {array} The flattened array.
62+
*/
63+
toNumberArray: function (output) {
64+
65+
if (typeof output === 'undefined') { output = []; }
66+
67+
for (var i = 0; i < this._points.length; i++)
68+
{
69+
if (typeof this._points[i] === 'number')
70+
{
71+
output.push(this._points[i]);
72+
output.push(this._points[i + 1]);
73+
i++;
74+
}
75+
else
76+
{
77+
output.push(this._points[i].x);
78+
output.push(this._points[i].y);
79+
}
80+
}
81+
82+
return output;
83+
84+
},
85+
86+
/**
87+
* Flattens this Polygon so the points are a sequence of numbers. Any Point objects found are removed and replaced with two numbers.
88+
*
89+
* @method Phaser.Polygon#flatten
90+
* @return {Phaser.Polygon} This Polygon object
91+
*/
92+
flatten: function () {
93+
94+
this._points = this.toNumberArray();
95+
96+
return this;
97+
98+
},
99+
56100
/**
57101
* Creates a copy of the given Polygon.
58102
* This is a deep clone, the resulting copy contains new Phaser.Point objects
@@ -149,12 +193,12 @@ Phaser.Polygon.prototype = {
149193
{
150194
if (typeof points[i] === 'number')
151195
{
152-
var p = new Phaser.Point(points[i], points[i + 1]);
196+
var p = new PIXI.Point(points[i], points[i + 1]);
153197
i++;
154198
}
155199
else
156200
{
157-
var p = new Phaser.Point(points[i].x, points[i].y);
201+
var p = new PIXI.Point(points[i].x, points[i].y);
158202
}
159203

160204
this._points.push(p);

0 commit comments

Comments
 (0)