Skip to content

Commit 8c23bca

Browse files
committed
Sprite.left, Sprite.right, Sprite.top, Sprite.bottom are new properties that contain the totals of the Sprite position and dimensions, adjusted for the anchor.
Sprite.offsetX and Sprite.offsetY contain the offsets from the Sprite.x/y coordinates to the top-left of the Sprite, taking anchor into consideration.
1 parent 01a23b7 commit 8c23bca

4 files changed

Lines changed: 227 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Thanks to @pnstickne for vast majority of this update.
100100
* TilemapLayer.setScale will allow you to apply scaling to a specific Tilemap layer, i.e. `layer.setScale(2)` would double the size of the layer. The way the Camera responds to the layer is adjusted accordingly based on the scale, as is Arcade collision (thanks @mickez #1605)
101101
* SoundManager.setDecodedCallback lets you specify a list of Sound files, or keys, and a callback. Once all of the Sound files have finished decoding the callback will be invoked. The amount of time spent decoding depends on the codec used and file size. If all of the files given have already decoded the callback is triggered immediately.
102102
* Sound.loopFull is a new method that will start playback of the Sound and set it to loop in its entirety.
103+
* Sprite.left, Sprite.right, Sprite.top, Sprite.bottom are new properties that contain the totals of the Sprite position and dimensions, adjusted for the anchor.
104+
* Sprite.offsetX and Sprite.offsetY contain the offsets from the Sprite.x/y coordinates to the top-left of the Sprite, taking anchor into consideration.
103105

104106
### Updates
105107

src/gameobjects/Image.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,112 @@ Object.defineProperty(Phaser.Image.prototype, "smoothed", {
10331033

10341034
});
10351035

1036+
/**
1037+
* The amount the image is visually offset from its x coordinate.
1038+
* This is the same as `Image.width * Image.anchor.x`.
1039+
* It will only be > 0 if the Image.anchor.x is not equal to zero.
1040+
*
1041+
* @name Phaser.Image#offsetX
1042+
* @property {number} offsetX - The amount the image is visually offset from its x coordinate.
1043+
* @readOnly
1044+
*/
1045+
Object.defineProperty(Phaser.Image.prototype, "offsetX", {
1046+
1047+
get: function () {
1048+
1049+
return this.anchor.x * this.width;
1050+
1051+
}
1052+
1053+
});
1054+
1055+
/**
1056+
* The amount the image is visually offset from its y coordinate.
1057+
* This is the same as `Image.height * Image.anchor.y`.
1058+
* It will only be > 0 if the Image.anchor.y is not equal to zero.
1059+
*
1060+
* @name Phaser.Image#offsetY
1061+
* @property {number} offsetY - The amount the image is visually offset from its y coordinate.
1062+
* @readOnly
1063+
*/
1064+
Object.defineProperty(Phaser.Image.prototype, "offsetY", {
1065+
1066+
get: function () {
1067+
1068+
return this.anchor.y * this.height;
1069+
1070+
}
1071+
1072+
});
1073+
1074+
/**
1075+
* The left coordinate of the Image, adjusted for the anchor.
1076+
*
1077+
* @name Phaser.Image#left
1078+
* @property {number} left - The left coordinate of the Image, adjusted for the anchor.
1079+
* @readOnly
1080+
*/
1081+
Object.defineProperty(Phaser.Image.prototype, "left", {
1082+
1083+
get: function () {
1084+
1085+
return this.x - this.offsetX;
1086+
1087+
}
1088+
1089+
});
1090+
1091+
/**
1092+
* The right coordinate of the Image, adjusted for the anchor. This is the same as Image.x + Image.width - Image.offsetX.
1093+
*
1094+
* @name Phaser.Image#right
1095+
* @property {number} right - The right coordinate of the Image, adjusted for the anchor. This is the same as Image.x + Image.width - Image.offsetX.
1096+
* @readOnly
1097+
*/
1098+
Object.defineProperty(Phaser.Image.prototype, "right", {
1099+
1100+
get: function () {
1101+
1102+
return (this.x + this.width) - this.offsetX;
1103+
1104+
}
1105+
1106+
});
1107+
1108+
/**
1109+
* The y coordinate of the Image, adjusted for the anchor.
1110+
*
1111+
* @name Phaser.Image#top
1112+
* @property {number} top - The y coordinate of the Image, adjusted for the anchor.
1113+
* @readOnly
1114+
*/
1115+
Object.defineProperty(Phaser.Image.prototype, "top", {
1116+
1117+
get: function () {
1118+
1119+
return this.y - this.offsetY;
1120+
1121+
}
1122+
1123+
});
1124+
1125+
/**
1126+
* The sum of the y and height properties, adjusted for the anchor.
1127+
*
1128+
* @name Phaser.Image#bottom
1129+
* @property {number} bottom - The sum of the y and height properties, adjusted for the anchor.
1130+
* @readOnly
1131+
*/
1132+
Object.defineProperty(Phaser.Image.prototype, "bottom", {
1133+
1134+
get: function () {
1135+
1136+
return (this.y + this.height) - this.offsetY;
1137+
1138+
}
1139+
1140+
});
1141+
10361142
/**
10371143
* @name Phaser.Image#destroyPhase
10381144
* @property {boolean} destroyPhase - True if this object is currently being destroyed.

src/gameobjects/Sprite.js

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
148148
this.debug = false;
149149

150150
/**
151-
* @property {Phaser.Point} cameraOffset - If this object is fixedToCamera then this stores the x/y offset that its drawn at, from the top-left of the camera view.
151+
* @property {Phaser.Point} cameraOffset - If this object is fixedToCamera then this stores the x/y offset that it is drawn at. Values are relative to the top-left of the camera view.
152152
*/
153153
this.cameraOffset = new Phaser.Point();
154154

@@ -1354,6 +1354,112 @@ Object.defineProperty(Phaser.Sprite.prototype, "y", {
13541354

13551355
});
13561356

1357+
/**
1358+
* The amount the sprite is visually offset from its x coordinate.
1359+
* This is the same as `Sprite.width * Sprite.anchor.x`.
1360+
* It will only be > 0 if the Sprite.anchor.x is not equal to zero.
1361+
*
1362+
* @name Phaser.Sprite#offsetX
1363+
* @property {number} offsetX - The amount the sprite is visually offset from its x coordinate.
1364+
* @readOnly
1365+
*/
1366+
Object.defineProperty(Phaser.Sprite.prototype, "offsetX", {
1367+
1368+
get: function () {
1369+
1370+
return this.anchor.x * this.width;
1371+
1372+
}
1373+
1374+
});
1375+
1376+
/**
1377+
* The amount the sprite is visually offset from its y coordinate.
1378+
* This is the same as `Sprite.height * Sprite.anchor.y`.
1379+
* It will only be > 0 if the Sprite.anchor.y is not equal to zero.
1380+
*
1381+
* @name Phaser.Sprite#offsetY
1382+
* @property {number} offsetY - The amount the sprite is visually offset from its y coordinate.
1383+
* @readOnly
1384+
*/
1385+
Object.defineProperty(Phaser.Sprite.prototype, "offsetY", {
1386+
1387+
get: function () {
1388+
1389+
return this.anchor.y * this.height;
1390+
1391+
}
1392+
1393+
});
1394+
1395+
/**
1396+
* The left coordinate of the Sprite, adjusted for the anchor.
1397+
*
1398+
* @name Phaser.Sprite#left
1399+
* @property {number} left - The left coordinate of the Sprite, adjusted for the anchor.
1400+
* @readOnly
1401+
*/
1402+
Object.defineProperty(Phaser.Sprite.prototype, "left", {
1403+
1404+
get: function () {
1405+
1406+
return this.x - this.offsetX;
1407+
1408+
}
1409+
1410+
});
1411+
1412+
/**
1413+
* The right coordinate of the Sprite, adjusted for the anchor. This is the same as Sprite.x + Sprite.width - Sprite.offsetX.
1414+
*
1415+
* @name Phaser.Sprite#right
1416+
* @property {number} right - The right coordinate of the Sprite, adjusted for the anchor. This is the same as Sprite.x + Sprite.width - Sprite.offsetX.
1417+
* @readOnly
1418+
*/
1419+
Object.defineProperty(Phaser.Sprite.prototype, "right", {
1420+
1421+
get: function () {
1422+
1423+
return (this.x + this.width) - this.offsetX;
1424+
1425+
}
1426+
1427+
});
1428+
1429+
/**
1430+
* The y coordinate of the Sprite, adjusted for the anchor.
1431+
*
1432+
* @name Phaser.Sprite#top
1433+
* @property {number} top - The y coordinate of the Sprite, adjusted for the anchor.
1434+
* @readOnly
1435+
*/
1436+
Object.defineProperty(Phaser.Sprite.prototype, "top", {
1437+
1438+
get: function () {
1439+
1440+
return this.y - this.offsetY;
1441+
1442+
}
1443+
1444+
});
1445+
1446+
/**
1447+
* The sum of the y and height properties, adjusted for the anchor.
1448+
*
1449+
* @name Phaser.Sprite#bottom
1450+
* @property {number} bottom - The sum of the y and height properties, adjusted for the anchor.
1451+
* @readOnly
1452+
*/
1453+
Object.defineProperty(Phaser.Sprite.prototype, "bottom", {
1454+
1455+
get: function () {
1456+
1457+
return (this.y + this.height) - this.offsetY;
1458+
1459+
}
1460+
1461+
});
1462+
13571463
/**
13581464
* @name Phaser.Sprite#destroyPhase
13591465
* @property {boolean} destroyPhase - True if this object is currently being destroyed.

typescript/phaser.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,7 @@ declare module Phaser {
14941494
anchor: Phaser.Point;
14951495
animations: Phaser.AnimationManager;
14961496
autoCull: boolean;
1497+
bottom: number;
14971498
cameraOffset: Phaser.Point;
14981499
cropRect: Phaser.Rectangle;
14991500
debug: boolean;
@@ -1512,11 +1513,16 @@ declare module Phaser {
15121513
inputEnabled: boolean;
15131514
inWorld: boolean;
15141515
key: string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture;
1516+
left: number;
15151517
name: string;
1518+
offsetX: number;
1519+
offsetY: number;
15161520
position: Phaser.Point;
15171521
renderOrderID: number;
1522+
right: number;
15181523
scale: Phaser.Point;
15191524
smoothed: boolean;
1525+
top: number;
15201526
type: number;
15211527
world: Phaser.Point;
15221528
z: number;
@@ -3789,6 +3795,7 @@ declare module Phaser {
37893795
animations: Phaser.AnimationManager;
37903796
autoCull: boolean;
37913797
body: any;
3798+
bottom: number;
37923799
cameraOffset: Phaser.Point;
37933800
checkWorldBounds: boolean;
37943801
cropRect: Phaser.Rectangle;
@@ -3809,16 +3816,21 @@ declare module Phaser {
38093816
inputEnabled: boolean;
38103817
inWorld: boolean;
38113818
key: string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture;
3819+
left: number;
38123820
lifespan: number;
38133821
name: string;
3822+
offsetX: number;
3823+
offsetY: number;
38143824
outOfBoundsKill: boolean;
38153825
position: Phaser.Point;
38163826
physicsEnabled: boolean;
38173827
renderOrderID: number;
3828+
right: number;
38183829
scale: Phaser.Point;
38193830
scaleMin: Phaser.Point;
38203831
scaleMax: Phaser.Point;
38213832
smoothed: boolean;
3833+
top: number;
38223834
type: number;
38233835
world: Phaser.Point;
38243836
x: number;

0 commit comments

Comments
 (0)