Skip to content

Commit 853d770

Browse files
committed
Group.align is a new method that allows you to layout all the children of the Group in a grid formation. You can specify the dimensions of the grid, including the width, height and cell size. You can also control where children are positioned within each grid cell. The grid width and height values can also be set to -1, making them fluid, so the grid expands until all children are aligned. Finally an optional child index argument can be set. This is a great way to quickly and comprehensively align Group children, and has lots of use cases.
1 parent fc7a393 commit 853d770

3 files changed

Lines changed: 43 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
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.
370370
* 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.
371371
* All Game Objects with the Bounds component; which includes Sprites, Images, Text, BitmapText, TileSprites and anything that extend these, now have a new method `alignTo`. It allows you to align the Game Object to another Game Object, or a Rectangle. You can specify one of 9 positions which are the new constants: `Phaser.TOP_LEFT`, `Phaser.TOP_CENTER` and so on (see above for the complete list). The Game Objects are positioned based on their Bounds, which takes rotation, scaling and anchor into consideration. You can easily place Sprites into the corners or the screen or game world, or align them against other Sprites, using this method.
372+
* Group.align is a new method that allows you to layout all the children of the Group in a grid formation. You can specify the dimensions of the grid, including the width, height and cell size. You can also control where children are positioned within each grid cell. The grid width and height values can also be set to -1, making them fluid, so the grid expands until all children are aligned. Finally an optional child index argument can be set. This is a great way to quickly and comprehensively align Group children, and has lots of use cases.
372373

373374
### Updates
374375

src/core/Group.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -674,29 +674,59 @@ Phaser.Group.prototype.updateZ = function () {
674674

675675
/**
676676
* This method iterates through all children in the Group (regardless if they are visible or exist)
677-
* and then changes their position properties so they are arranged in a Grid formation.
677+
* and then changes their position so they are arranged in a Grid formation. Children must have
678+
* the `alignTo` method in order to be positioned by this call. All default Phaser Game Objects have
679+
* this.
678680
*
679-
* The grid dimensions are determined by the first four arguments. The rows and columns arguments
681+
* The grid dimensions are determined by the first four arguments. The `rows` and `columns` arguments
680682
* relate to the width and height of the grid respectively.
683+
*
684+
* For example if the Group had 100 children in it:
685+
*
686+
* `Group.align(10, 10, 32, 32)`
687+
*
688+
* This will align all of the children into a grid formation of 10x10, using 32 pixels per
689+
* grid cell. If you want a wider grid, you could do:
681690
*
682-
* You can choose to set 'columns' to -1.
683-
* If you do this it means it will continue aligning all of the children of the Group
691+
* `Group.align(25, 4, 32, 32)`
692+
*
693+
* This will align the children into a grid of 25x4, again using 32 pixels per grid cell.
694+
*
695+
* You can choose to set _either_ the `rows` or `columns` value to -1. Doing so tells the method
696+
* to keep on aligning children until there are no children left. For example if this Group had
697+
* 48 children in it, the following:
698+
*
699+
* `Group.align(-1, 8, 32, 32)`
700+
*
701+
* ... will align the children so that there are 8 columns vertically (the second argument),
702+
* and each row will contain 6 sprites, except the last one, which will contain 5 (totaling 48)
703+
*
704+
* You can also do:
684705
*
685-
* This is the number of grid cells that exist
686-
* in total.
706+
* `Group.align(10, -1, 32, 32)`
707+
*
708+
* In this case it will create a grid 10 wide, and as tall as it needs to be in order to fit
709+
* all of the children in.
710+
*
711+
* The `position` property allows you to control where in each grid cell the child is positioned.
712+
* This is a constant, such as `Phaser.TOP_RIGHT` or `Phaser.MIDDLE_CENTER`.
713+
*
714+
* The final argument; `offset` lets you start the alignment from a specific child index.
687715
*
688716
* @method Phaser.Group#align
689-
* @param {integer} rows - The number of rows to use in the grid alignment.
690-
* @param {integer} columns - The number of columns to use in the grid alignment.
717+
* @param {integer} rows - The number of rows, or width, of the grid. Set to -1 for a dynamic width.
718+
* @param {integer} columns - The number of columns, or height, of the grid. Set to -1 for a dynamic height.
691719
* @param {integer} cellWidth - The width of each grid cell, in pixels.
692720
* @param {integer} cellHeight - The height of each grid cell, in pixels.
693721
* @param {integer} [position] - The position constant. One of `Phaser.TOP_LEFT` (default), `Phaser.TOP_CENTER`, `Phaser.TOP_RIGHT`, `Phaser.MIDDLE_LEFT`, `Phaser.MIDDLE_CENTER`, `Phaser.MIDDLE_RIGHT`, `Phaser.BOTTOM_LEFT`, `Phaser.BOTTOM_CENTER` or `Phaser.BOTTOM_RIGHT`.
722+
* @param {integer} [offset=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.
694723
*/
695-
Phaser.Group.prototype.align = function (rows, columns, cellWidth, cellHeight, position) {
724+
Phaser.Group.prototype.align = function (rows, columns, cellWidth, cellHeight, position, offset) {
696725

697726
if (position === undefined) { position = Phaser.TOP_LEFT; }
727+
if (offset === undefined) { offset = 0; }
698728

699-
if (this.children.length === 0 || (rows === -1 && columns === -1))
729+
if (this.children.length === 0 || offset > this.children.length || (rows === -1 && columns === -1))
700730
{
701731
return;
702732
}
@@ -705,7 +735,7 @@ Phaser.Group.prototype.align = function (rows, columns, cellWidth, cellHeight, p
705735
var w = (rows * cellWidth);
706736
var h = (columns * cellHeight);
707737

708-
for (var i = 0; i < this.children.length; i++)
738+
for (var i = offset; i < this.children.length; i++)
709739
{
710740
var child = this.children[i];
711741

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,7 @@ declare module "phaser" {
17301730
addAt(child: any, index: number, silent?: boolean): any;
17311731
addMultiple(children: any[], silent?: boolean): any[];
17321732
addToHash(child: PIXI.DisplayObject): boolean;
1733+
align(rows: number, columns: number, cellWidth: number, cellHeight: number, position?: number, offset?: number): void;
17331734
bringToTop(child: any): any;
17341735
callAll(method: string, context: any, ...parameters: any[]): void;
17351736
callAllExists(callback: string, existsValue: boolean, ...parameters: any[]): void;

0 commit comments

Comments
 (0)