Skip to content

Commit b24de1e

Browse files
committed
Polygon.contains would only work with non-flattened Polygon objects. It now works with both flat and non-flat Polygons.
Graphics objects enabled for input would fail to do anything if a Phaser Polygon was given to the Graphics object (which it was in nearly all cases), as it wouldn't detect input correctly with flattened polygons (thanks @symbiane phaserjs#2591)
1 parent 21ad151 commit b24de1e

4 files changed

Lines changed: 44 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
336336
* Fixed issue in Group.align where the cell wouldn't increase if `rows` was great than -1
337337
* Sound.volume was accidentally repeated twice in the source (thanks @LoneStranger #2569)
338338
* Animation.setFrame wouldn't work correctly if the `useLocalFrameIndex` argument was true, and the frame ID was a number (thanks @uboot #2571)
339+
* Polygon.contains would only work with non-flattened Polygon objects. It now works with both flat and non-flat Polygons.
340+
* Graphics objects enabled for input would fail to do anything if a Phaser Polygon was given to the Graphics object (which it was in nearly all cases), as it wouldn't detect input correctly with flattened polygons (thanks @symbiane #2591)
339341

340342
### Pixi Updates
341343

src/geom/Polygon.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ Phaser.Polygon = function () {
4444
*/
4545
this.closed = true;
4646

47+
/**
48+
* @property {boolean} flattened - Has this Polygon been flattened by a call to `Polygon.flatten` ?
49+
*/
50+
this.flattened = false;
51+
4752
/**
4853
* @property {number} type - The base object type.
4954
*/
@@ -84,7 +89,9 @@ Phaser.Polygon.prototype = {
8489
},
8590

8691
/**
87-
* Flattens this Polygon so the points are a sequence of numbers. Any Point objects found are removed and replaced with two numbers.
92+
* Flattens this Polygon so the points are a sequence of numbers.
93+
* Any Point objects found are removed and replaced with two numbers.
94+
* Also sets the Polygon.flattened property to `true`.
8895
*
8996
* @method Phaser.Polygon#flatten
9097
* @return {Phaser.Polygon} This Polygon object
@@ -93,6 +100,8 @@ Phaser.Polygon.prototype = {
93100

94101
this._points = this.toNumberArray();
95102

103+
this.flattened = true;
104+
96105
return this;
97106

98107
},
@@ -134,20 +143,39 @@ Phaser.Polygon.prototype = {
134143

135144
// Adapted from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html by Jonas Raoni Soares Silva
136145

137-
var length = this._points.length;
138146
var inside = false;
139147

140-
for (var i = -1, j = length - 1; ++i < length; j = i)
148+
if (this.flattened)
141149
{
142-
var ix = this._points[i].x;
143-
var iy = this._points[i].y;
150+
for (var i = -2, j = this._points.length - 2; (i += 2) < this._points.length; j = i)
151+
{
152+
var ix = this._points[i];
153+
var iy = this._points[i + 1];
154+
155+
var jx = this._points[j];
156+
var jy = this._points[j + 1];
144157

145-
var jx = this._points[j].x;
146-
var jy = this._points[j].y;
158+
if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix))
159+
{
160+
inside = !inside;
161+
}
162+
}
147163

148-
if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix))
164+
}
165+
else
166+
{
167+
for (var i = -1, j = this._points.length - 1; ++i < this._points.length; j = i)
149168
{
150-
inside = !inside;
169+
var ix = this._points[i].x;
170+
var iy = this._points[i].y;
171+
172+
var jx = this._points[j].x;
173+
var jy = this._points[j].y;
174+
175+
if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix))
176+
{
177+
inside = !inside;
178+
}
151179
}
152180
}
153181

src/input/InputHandler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,10 @@ Phaser.InputHandler.prototype = {
283283

284284
/**
285285
* Starts the Input Handler running. This is called automatically when you enable input on a Sprite, or can be called directly if you need to set a specific priority.
286+
*
286287
* @method Phaser.InputHandler#start
287-
* @param {number} priority - Higher priority sprites take click priority over low-priority sprites when they are stacked on-top of each other.
288-
* @param {boolean} useHandCursor - If true the Sprite will show the hand cursor on mouse-over (doesn't apply to mobile browsers)
288+
* @param {number} [priority=0] - Higher priority sprites take click priority over low-priority sprites when they are stacked on-top of each other.
289+
* @param {boolean} [useHandCursor=false] - If true the Sprite will show the hand cursor on mouse-over (doesn't apply to mobile browsers)
289290
* @return {Phaser.Sprite} The Sprite object to which the Input Handler is bound.
290291
*/
291292
start: function (priority, useHandCursor) {

typescript/phaser.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path="pixi.d.ts" />
22
/// <reference path="p2.d.ts" />
33

4-
// Type definitions for Phaser 2.5.1 - 21st June 2016
4+
// Type definitions for Phaser 2.5.1 - 27th June 2016
55
// Project: https://github.com/photonstorm/phaser
66

77
declare module "phaser" {
@@ -4009,6 +4009,7 @@ declare module Phaser {
40094009
constructor(...points: number[]);
40104010

40114011
area: number;
4012+
flattened: boolean;
40124013
points: number[] | Phaser.Point[];
40134014
type: number;
40144015

0 commit comments

Comments
 (0)