You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -369,6 +369,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
369
369
* 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.
370
370
* 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.
371
371
* 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.
Copy file name to clipboardExpand all lines: src/core/Group.js
+41-11Lines changed: 41 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -674,29 +674,59 @@ Phaser.Group.prototype.updateZ = function () {
674
674
675
675
/**
676
676
* 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.
678
680
*
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
680
682
* 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:
681
690
*
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:
684
705
*
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.
687
715
*
688
716
* @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.
691
719
* @param {integer} cellWidth - The width of each grid cell, in pixels.
692
720
* @param {integer} cellHeight - The height of each grid cell, in pixels.
693
721
* @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.
0 commit comments