When I made Monstro in the first place I used Starling as the rendering framework but because I’ve earlier worked by directly modifying BitmapData I had my own set of Display Objects and their properties. One of the properties I needed in Starling was “center” which retrieves the center position of a sprite and sets it, a thing that can be easily replaced by
x = value - width / 2 but the resulting code is not as clear and is much more prone to a mean typo.
Since for Starling I just made my own fork I didn’t really have a problem there, but for Phaser I decided to stick to the main repository to avoid the pain of having to keep it up to date and eventually growing in a completely different direction and being 500% unhappy.
It includes such augmentations:
positionCenter/positionMiddle– This function positions the object exactly in the horizontal center/vertical middle of the screen (it takes the first
Phaser.Gameobject to get the screen dimensions).
positionCenterParent/positionMiddleParent– Like the above but it uses the parent of the object instead of the whole screen.
right/bottom– Get/set the position of the right/bottom edge of the object.
center/middle– Get/set the position of the center/middle of the object.
globalToLocalX/globalToLocalY– Given the global position X/Y it returns the said position relative to the object’s local space.
If, like me, you’re a TypeScript user, the Gist also includes a definition file with references to the augmentation files. Just make sure to reference it in your bootstrap or whatever after you reference Phaser. Unfortunately th version of TypeScript I am using does not allow you to define the same class in multiple places, so I had to modify my
phaser.d.ts file manually to add the augmentations.