Skip to content

Commit 0efcf68

Browse files
committed
The Game Object Bounds component has been updated to include two new properties: centerX and centerY. This means you can, for example, now get the horizontal center of a Sprite by called Sprite.centerX. These properties are also setters, so you can position the Game Objects, and it will take scale and anchor into consideration.
1 parent 123e61c commit 0efcf68

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
367367
* BitmapData.drawGroup now uses the new `copyTransform` method, to provide for far more accurate results. Previously nested Game Objects wouldn't render correctly, nor would Sprites added via `addChild` to another Sprite. BitmapText objects also rendered without rotation taken into account, and the Sprites smoothing property was ignored. All of these things are now covered by the new drawGroup method, which also handles full deep iteration down the display list.
368368
* Added the following new constants: `Phaser.TOP_LEFT`, `Phaser.TOP_CENTER`, `Phaser.TOP_RIGHT`, `Phaser.MIDDLE_LEFT`, `Phaser.MIDDLE_CENTER`, `Phaser.MIDDLE_RIGHT`, `Phaser.BOTTOM_LEFT`, `Phaser.BOTTOM_CENTER` and `Phaser.BOTTOM_RIGHT`.
369369
* Rectangle.getPoint is a new method that returns a point based on the given location constant, such as `Phaser.BOTTOM_LEFT`. It returns the same result as calling `Rectangle.bottomLeft` (etc) but unlike those getters you are able to provide your own Point object.
370+
* The Game Object Bounds component has been updated to include two new properties: `centerX` and `centerY`. This means you can, for example, now get the horizontal center of a Sprite by called `Sprite.centerX`. These properties are also setters, so you can position the Game Objects, and it will take scale and anchor into consideration.
370371

371372
### Updates
372373

src/gameobjects/components/Bounds.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,50 @@ Phaser.Component.Bounds.prototype = {
4949

5050
},
5151

52+
/**
53+
* The center x coordinate of the Game Object.
54+
* This is the same as `(x - offsetX) + (width / 2)`.
55+
*
56+
* @property {number} centerX
57+
*/
58+
centerX: {
59+
60+
get: function () {
61+
62+
return (this.x - this.offsetX) + (this.width * 0.5);
63+
64+
},
65+
66+
set: function (value) {
67+
68+
this.x = (value + this.offsetX) - (this.width * 0.5);
69+
70+
}
71+
72+
},
73+
74+
/**
75+
* The center y coordinate of the Game Object.
76+
* This is the same as `(y - offsetY) + (height / 2)`.
77+
*
78+
* @property {number} centerY
79+
*/
80+
centerY: {
81+
82+
get: function () {
83+
84+
return (this.y - this.offsetY) + (this.height * 0.5);
85+
86+
},
87+
88+
set: function (value) {
89+
90+
this.y = (value + this.offsetY) - (this.height * 0.5);
91+
92+
}
93+
94+
},
95+
5296
/**
5397
* The left coordinate of the Game Object.
5498
* This is the same as `x - offsetX`.

typescript/phaser.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,8 @@ declare module "phaser" {
18001800
autoCull: boolean;
18011801
bottom: number;
18021802
cameraOffset: Phaser.Point;
1803+
centerX: number;
1804+
centerY: number;
18031805
components: any;
18041806
cropRect: Phaser.Rectangle;
18051807
customRender: boolean;
@@ -4475,6 +4477,8 @@ declare module "phaser" {
44754477
body: Phaser.Physics.Arcade.Body | Phaser.Physics.P2.Body | Phaser.Physics.Ninja.Body | any;
44764478
bottom: number;
44774479
cameraOffset: Phaser.Point;
4480+
centerX: number;
4481+
centerY: number;
44784482
checkWorldBounds: boolean;
44794483
components: any;
44804484
cropRect: Phaser.Rectangle;

0 commit comments

Comments
 (0)