Line.centerOn will position the Line so that its midpoint lays on the coordinates given.

This commit is contained in:
Richard Davey 2015-08-07 01:40:37 +01:00
parent 614b84e2ea
commit 7588e123c7
3 changed files with 17 additions and 4 deletions

View file

@ -258,6 +258,7 @@ If you are an exceptional JavaScript developer and would like to join the Phaser
* Sprite.setTexture has a new `destroyBase` parameter - set this to `true` if you know the base used a generated texture that isn't being used by any other sprites. This will free-up the canvas for further re-use by other calls to generateTexture or Text objects.
* Line.midPoint will return a Point object where the x and y values correspond to the center (or midpoint) of the Line segment.
* Line.rotateAround allows you to rotate a Line around the given coordinates (in world space)
* Line.centerOn will position the Line so that its midpoint lays on the coordinates given.
### Updates

View file

@ -195,13 +195,24 @@ Phaser.Line.prototype = {
},
/**
* Centers this Line on the given coordinates.
*
* The line is centered by positioning the start and end points so that the lines midpoint matches
* the coordinates given.
*
* @method Phaser.Line#centerOn
* @param {number} x - The x position to center the line on.
* @param {number} y - The y position to center the line on.
* @return {Phaser.Line} This line object
*/
centerOn: function (x, y) {
var midx = (this.start.x + this.end.x) / 2;
var midy = (this.start.y + this.end.y) / 2;
var cx = (this.start.x + this.end.x) / 2;
var cy = (this.start.y + this.end.y) / 2;
var tx = x - midx;
var ty = y - midy;
var tx = x - cx;
var ty = y - cy;
this.start.add(tx, ty);
this.end.add(tx, ty);

View file

@ -2125,6 +2125,7 @@ declare module Phaser {
static intersects(a: Phaser.Line, b: Phaser.Line, asSegment?: boolean, result?: Phaser.Point): Phaser.Point;
static reflect(a: Phaser.Line, b: Phaser.Line): number;
centerOn(x: number, y: number): Phaser.Line;
clone(output: Phaser.Line): Phaser.Line;
coordinatesOnLine(stepRate: number, results: any[]): any[];
fromAngle(x: number, y: number, angle: number, length: number): Phaser.Line;