FlexLayer#align()

align(width, height, cellWidth, cellHeight, position, offset) → {boolean}

This method iterates through all children in the Group (regardless if they are visible or exist)
and then changes their position so they are arranged in a Grid formation. Children must have
the alignTo method in order to be positioned by this call. All default Phaser Game Objects have
this.

The grid dimensions are determined by the first four arguments. The width and height arguments
relate to the width and height of the grid respectively.

For example if the Group had 100 children in it:

Group.align(10, 10, 32, 32)

This will align all of the children into a grid formation of 10x10, using 32 pixels per
grid cell. If you want a wider grid, you could do:

Group.align(25, 4, 32, 32)

This will align the children into a grid of 25x4, again using 32 pixels per grid cell.

You can choose to set either the width or height value to -1. Doing so tells the method
to keep on aligning children until there are no children left. For example if this Group had
48 children in it, the following:

Group.align(-1, 8, 32, 32)

... will align the children so that there are 8 children vertically (the second argument),
and each row will contain 6 sprites, except the last one, which will contain 5 (totaling 48)

You can also do:

Group.align(10, -1, 32, 32)

In this case it will create a grid 10 wide, and as tall as it needs to be in order to fit
all of the children in.

The position property allows you to control where in each grid cell the child is positioned.
This is a constant and can be 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.

The final argument; offset lets you start the alignment from a specific child index.

Parameters
Name Type Argument Default Description
width integer

The width of the grid in items (not pixels). Set to -1 for a dynamic width. If -1 then you must set an explicit height value.

height integer

The height of the grid in items (not pixels). Set to -1 for a dynamic height. If -1 then you must set an explicit width value.

cellWidth integer

The width of each grid cell, in pixels.

cellHeight integer

The height of each grid cell, in pixels.

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.

offset integer <optional>
0

Optional index to start the alignment from. Defaults to zero, the first child in the Group, but can be set to any valid child index value.

Returns
boolean -

True if the Group children were aligned, otherwise false.

Inherited From
Source code: core/Group.js (Line 682)
doc_phaser
2017-02-14 10:46:17
Comments
Leave a Comment

Please login to continue.