Navigation
These archived docs are for Phaser 2.6.1 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser.Component. Bounds

new Bounds()

The Bounds component contains properties related to the bounds of the Game Object.

Source code: gameobjects/components/Bounds.js (Line 12)

Public Properties

bottom : number

The sum of the y and height properties. This is the same as y + height - offsetY.

Source code: gameobjects/components/Bounds.js (Line 168)

centerX : number

The center x coordinate of the Game Object. This is the same as (x - offsetX) + (width / 2).

Source code: gameobjects/components/Bounds.js (Line 58)

centerY : number

The center y coordinate of the Game Object. This is the same as (y - offsetY) + (height / 2).

Source code: gameobjects/components/Bounds.js (Line 80)

left : number

The left coordinate of the Game Object. This is the same as x - offsetX.

Source code: gameobjects/components/Bounds.js (Line 102)

[readonly] offsetX : number

The amount the Game Object is visually offset from its x coordinate. This is the same as width * anchor.x. It will only be > 0 if anchor.x is not equal to zero.

Source code: gameobjects/components/Bounds.js (Line 24)

[readonly] offsetY : number

The amount the Game Object is visually offset from its y coordinate. This is the same as height * anchor.y. It will only be > 0 if anchor.y is not equal to zero.

Source code: gameobjects/components/Bounds.js (Line 42)

The right coordinate of the Game Object. This is the same as x + width - offsetX.

Source code: gameobjects/components/Bounds.js (Line 124)

top : number

The y coordinate of the Game Object. This is the same as y - offsetY.

Source code: gameobjects/components/Bounds.js (Line 146)

Public Methods

alignIn(container, position, offsetX, offsetY) → {Object}

Aligns this Game Object within another Game Object, or Rectangle, known as the 'container', to one of 9 possible positions.

The container must be a Game Object, or Phaser.Rectangle object. This can include properties such as World.bounds or Camera.view, for aligning Game Objects within the world and camera bounds. Or it can include other Sprites, Images, Text objects, BitmapText, TileSprites or Buttons.

Please note that aligning a Sprite to another Game Object does not make it a child of the container. It simply modifies its position coordinates so it aligns with it.

The position constants you can use are:

Phaser.TOP_LEFT, Phaser.TOP_CENTER, Phaser.TOP_RIGHT, Phaser.LEFT_CENTER, Phaser.CENTER, Phaser.RIGHT_CENTER, Phaser.BOTTOM_LEFT, Phaser.BOTTOM_CENTER and Phaser.BOTTOM_RIGHT.

The Game Objects are placed in such a way that their bounds align with the container, taking into consideration rotation, scale and the anchor property. This allows you to neatly align Game Objects, irrespective of their position value.

The optional offsetX and offsetY arguments allow you to apply extra spacing to the final aligned position of the Game Object. For example:

sprite.alignIn(background, Phaser.BOTTOM_RIGHT, -20, -20)

Would align the sprite to the bottom-right, but moved 20 pixels in from the corner. Think of the offsets as applying an adjustment to the containers bounds before the alignment takes place. So providing a negative offset will 'shrink' the container bounds by that amount, and providing a positive one expands it.

Parameters
Name Type Argument Default Description
container Phaser.Rectangle | Phaser.Sprite | Phaser.Image | Phaser.Text | Phaser.BitmapText | Phaser.Button | Phaser.Graphics | Phaser.TileSprite

The Game Object or Rectangle with which to align this Game Object to. Can also include properties such as World.bounds or Camera.view.

position integer <optional>

The position constant. One of Phaser.TOP_LEFT (default), Phaser.TOP_CENTER, Phaser.TOP_RIGHT, Phaser.LEFT_CENTER, Phaser.CENTER, Phaser.RIGHT_CENTER, Phaser.BOTTOM_LEFT, Phaser.BOTTOM_CENTER or Phaser.BOTTOM_RIGHT.

offsetX integer <optional>
0

A horizontal adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it.

offsetY integer <optional>
0

A vertical adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it.

Returns
Object -

This Game Object.

Source code: gameobjects/components/Bounds.js (Line 223)

alignTo(parent, position, offsetX, offsetY) → {Object}

Aligns this Game Object to the side of another Game Object, or Rectangle, known as the 'parent', in one of 11 possible positions.

The parent must be a Game Object, or Phaser.Rectangle object. This can include properties such as World.bounds or Camera.view, for aligning Game Objects within the world and camera bounds. Or it can include other Sprites, Images, Text objects, BitmapText, TileSprites or Buttons.

Please note that aligning a Sprite to another Game Object does not make it a child of the parent. It simply modifies its position coordinates so it aligns with it.

The position constants you can use are:

Phaser.TOP_LEFT (default), Phaser.TOP_CENTER, Phaser.TOP_RIGHT, Phaser.LEFT_TOP, Phaser.LEFT_CENTER, Phaser.LEFT_BOTTOM, Phaser.RIGHT_TOP, Phaser.RIGHT_CENTER, Phaser.RIGHT_BOTTOM, Phaser.BOTTOM_LEFT, Phaser.BOTTOM_CENTER and Phaser.BOTTOM_RIGHT.

The Game Objects are placed in such a way that their bounds align with the parent, taking into consideration rotation, scale and the anchor property. This allows you to neatly align Game Objects, irrespective of their position value.

The optional offsetX and offsetY arguments allow you to apply extra spacing to the final aligned position of the Game Object. For example:

sprite.alignTo(background, Phaser.BOTTOM_RIGHT, -20, -20)

Would align the sprite to the bottom-right, but moved 20 pixels in from the corner. Think of the offsets as applying an adjustment to the parents bounds before the alignment takes place. So providing a negative offset will 'shrink' the parent bounds by that amount, and providing a positive one expands it.

Parameters
Name Type Argument Default Description
parent Phaser.Rectangle | Phaser.Sprite | Phaser.Image | Phaser.Text | Phaser.BitmapText | Phaser.Button | Phaser.Graphics | Phaser.TileSprite

The Game Object or Rectangle with which to align this Game Object to. Can also include properties such as World.bounds or Camera.view.

position integer <optional>

The position constant. One of Phaser.TOP_LEFT, Phaser.TOP_CENTER, Phaser.TOP_RIGHT, Phaser.LEFT_TOP, Phaser.LEFT_CENTER, Phaser.LEFT_BOTTOM, Phaser.RIGHT_TOP, Phaser.RIGHT_CENTER, Phaser.RIGHT_BOTTOM, Phaser.BOTTOM_LEFT, Phaser.BOTTOM_CENTER or Phaser.BOTTOM_RIGHT.

offsetX integer <optional>
0

A horizontal adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it.

offsetY integer <optional>
0

A vertical adjustment of the Containers bounds, applied to the aligned position of the Game Object. Use a negative value to shrink the bounds, positive to increase it.

Returns
Object -

This Game Object.

Source code: gameobjects/components/Bounds.js (Line 321)