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 #2591)
This commit is contained in:
photonstorm 2016-06-27 14:51:25 +01:00
parent 21ad151513
commit b24de1e561
4 changed files with 46 additions and 14 deletions

View file

@ -336,6 +336,8 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
* Fixed issue in Group.align where the cell wouldn't increase if `rows` was great than -1
* Sound.volume was accidentally repeated twice in the source (thanks @LoneStranger #2569)
* Animation.setFrame wouldn't work correctly if the `useLocalFrameIndex` argument was true, and the frame ID was a number (thanks @uboot #2571)
* 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 #2591)
### Pixi Updates

View file

@ -44,6 +44,11 @@ Phaser.Polygon = function () {
*/
this.closed = true;
/**
* @property {boolean} flattened - Has this Polygon been flattened by a call to `Polygon.flatten` ?
*/
this.flattened = false;
/**
* @property {number} type - The base object type.
*/
@ -84,7 +89,9 @@ Phaser.Polygon.prototype = {
},
/**
* Flattens this Polygon so the points are a sequence of numbers. Any Point objects found are removed and replaced with two numbers.
* Flattens this Polygon so the points are a sequence of numbers.
* Any Point objects found are removed and replaced with two numbers.
* Also sets the Polygon.flattened property to `true`.
*
* @method Phaser.Polygon#flatten
* @return {Phaser.Polygon} This Polygon object
@ -93,6 +100,8 @@ Phaser.Polygon.prototype = {
this._points = this.toNumberArray();
this.flattened = true;
return this;
},
@ -134,20 +143,39 @@ Phaser.Polygon.prototype = {
// Adapted from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html by Jonas Raoni Soares Silva
var length = this._points.length;
var inside = false;
for (var i = -1, j = length - 1; ++i < length; j = i)
if (this.flattened)
{
var ix = this._points[i].x;
var iy = this._points[i].y;
var jx = this._points[j].x;
var jy = this._points[j].y;
if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix))
for (var i = -2, j = this._points.length - 2; (i += 2) < this._points.length; j = i)
{
inside = !inside;
var ix = this._points[i];
var iy = this._points[i + 1];
var jx = this._points[j];
var jy = this._points[j + 1];
if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix))
{
inside = !inside;
}
}
}
else
{
for (var i = -1, j = this._points.length - 1; ++i < this._points.length; j = i)
{
var ix = this._points[i].x;
var iy = this._points[i].y;
var jx = this._points[j].x;
var jy = this._points[j].y;
if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix))
{
inside = !inside;
}
}
}

View file

@ -283,9 +283,10 @@ Phaser.InputHandler.prototype = {
/**
* 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.
*
* @method Phaser.InputHandler#start
* @param {number} priority - Higher priority sprites take click priority over low-priority sprites when they are stacked on-top of each other.
* @param {boolean} useHandCursor - If true the Sprite will show the hand cursor on mouse-over (doesn't apply to mobile browsers)
* @param {number} [priority=0] - Higher priority sprites take click priority over low-priority sprites when they are stacked on-top of each other.
* @param {boolean} [useHandCursor=false] - If true the Sprite will show the hand cursor on mouse-over (doesn't apply to mobile browsers)
* @return {Phaser.Sprite} The Sprite object to which the Input Handler is bound.
*/
start: function (priority, useHandCursor) {

View file

@ -1,7 +1,7 @@
/// <reference path="pixi.d.ts" />
/// <reference path="p2.d.ts" />
// Type definitions for Phaser 2.5.1 - 21st June 2016
// Type definitions for Phaser 2.5.1 - 27th June 2016
// Project: https://github.com/photonstorm/phaser
declare module "phaser" {
@ -4009,6 +4009,7 @@ declare module Phaser {
constructor(...points: number[]);
area: number;
flattened: boolean;
points: number[] | Phaser.Point[];
type: number;