Skip to content

Commit 60b8d78

Browse files
committed
Group.align updates.
1 parent 0e24b1b commit 60b8d78

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
321321
* The InputHandler.flagged property has been removed. It was never used internally, or exposed via the API, so was just overhead.
322322
* The src/system folder has been removed and all files relocated to the src/utils folder. This doesn't change anything from an API point of view, but did change the grunt build scripts slightly.
323323
* BitmapData.shadow and BitmapData.text now both `return this` keeping them in-line with the docs (thanks @greeny #2634)
324-
* Group.align has had its argument orders swapped, so that it's now `(columns, rows, ...)` instead of `(rows, columns, ...)` (thanks @deargle #2643)
324+
* Group.align has had its arguments changed so that it's now `(width, height, ...)` instead of `(rows, columns, ...)` (thanks @deargle #2643)
325+
* Group.align now returns `true` if the Group was aligned, or `false` if not.
325326

326327
### Bug Fixes
327328

src/core/Group.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ Phaser.Group.prototype.updateZ = function () {
685685
* the `alignTo` method in order to be positioned by this call. All default Phaser Game Objects have
686686
* this.
687687
*
688-
* The grid dimensions are determined by the first four arguments. The `rows` and `columns` arguments
688+
* The grid dimensions are determined by the first four arguments. The `width` and `height` arguments
689689
* relate to the width and height of the grid respectively.
690690
*
691691
* For example if the Group had 100 children in it:
@@ -699,13 +699,13 @@ Phaser.Group.prototype.updateZ = function () {
699699
*
700700
* This will align the children into a grid of 25x4, again using 32 pixels per grid cell.
701701
*
702-
* You can choose to set _either_ the `rows` or `columns` value to -1. Doing so tells the method
702+
* You can choose to set _either_ the `width` or `height` value to -1. Doing so tells the method
703703
* to keep on aligning children until there are no children left. For example if this Group had
704704
* 48 children in it, the following:
705705
*
706706
* `Group.align(-1, 8, 32, 32)`
707707
*
708-
* ... will align the children so that there are 8 rows vertically (the second argument),
708+
* ... will align the children so that there are 8 children vertically (the second argument),
709709
* and each row will contain 6 sprites, except the last one, which will contain 5 (totaling 48)
710710
*
711711
* You can also do:
@@ -723,26 +723,27 @@ Phaser.Group.prototype.updateZ = function () {
723723
* The final argument; `offset` lets you start the alignment from a specific child index.
724724
*
725725
* @method Phaser.Group#align
726-
* @param {integer} columns - The number of columns, or width, of the grid. Set to -1 for a dynamic width.
727-
* @param {integer} rows - The number of rows, or height, of the grid. Set to -1 for a dynamic height.
726+
* @param {integer} width - 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.
727+
* @param {integer} height - 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.
728728
* @param {integer} cellWidth - The width of each grid cell, in pixels.
729729
* @param {integer} cellHeight - The height of each grid cell, in pixels.
730730
* @param {integer} [position] - 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`.
731731
* @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.
732+
* @return {boolean} True if the Group children were aligned, otherwise false.
732733
*/
733-
Phaser.Group.prototype.align = function (columns, rows, cellWidth, cellHeight, position, offset) {
734+
Phaser.Group.prototype.align = function (width, height, cellWidth, cellHeight, position, offset) {
734735

735736
if (position === undefined) { position = Phaser.TOP_LEFT; }
736737
if (offset === undefined) { offset = 0; }
737738

738-
if (this.children.length === 0 || offset > this.children.length || (columns === -1 && rows === -1))
739+
if (this.children.length === 0 || offset > this.children.length || (width === -1 && height === -1))
739740
{
740-
return;
741+
return false;
741742
}
742743

743744
var r = new Phaser.Rectangle(0, 0, cellWidth, cellHeight);
744-
var w = (columns * cellWidth);
745-
var h = (rows * cellHeight);
745+
var w = (width * cellWidth);
746+
var h = (height * cellHeight);
746747

747748
for (var i = offset; i < this.children.length; i++)
748749
{
@@ -757,7 +758,7 @@ Phaser.Group.prototype.align = function (columns, rows, cellWidth, cellHeight, p
757758
continue;
758759
}
759760

760-
if (columns === -1)
761+
if (width === -1)
761762
{
762763
// We keep laying them out horizontally until we've done them all
763764
r.y += cellHeight;
@@ -768,7 +769,7 @@ Phaser.Group.prototype.align = function (columns, rows, cellWidth, cellHeight, p
768769
r.y = 0;
769770
}
770771
}
771-
else if (rows === -1)
772+
else if (height === -1)
772773
{
773774
// We keep laying them out vertically until we've done them all
774775
r.x += cellWidth;
@@ -792,12 +793,14 @@ Phaser.Group.prototype.align = function (columns, rows, cellWidth, cellHeight, p
792793
if (r.y === h)
793794
{
794795
// We've hit the column limit, so return, even if there are children left
795-
return;
796+
return true;
796797
}
797798
}
798799
}
799800
}
800801

802+
return true;
803+
801804
};
802805

803806
/**

typescript/phaser.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,7 @@ declare module Phaser {
17191719
static RETURN_CHILD: number;
17201720
static RETURN_NONE: number;
17211721
static RETURN_TOTAL: number;
1722+
static RETURN_ALL: number;
17221723
static SORT_ASCENDING: number;
17231724
static SORT_DESCENDING: number;
17241725

@@ -1767,7 +1768,7 @@ declare module Phaser {
17671768
addAt(child: any, index: number, silent?: boolean): any;
17681769
addMultiple(children: any[], silent?: boolean): any[];
17691770
addToHash(child: PIXI.DisplayObject): boolean;
1770-
align(rows: number, columns: number, cellWidth: number, cellHeight: number, position?: number, offset?: number): void;
1771+
align(width: number, height: number, cellWidth: number, cellHeight: number, position?: number, offset?: number): boolean;
17711772
alignIn(container: Phaser.Rectangle | Phaser.Sprite | Phaser.Image | Phaser.Text | Phaser.BitmapText | Phaser.Button | Phaser.Graphics | Phaser.TileSprite, position?: number, offsetX?: number, offsetY?: number): Phaser.Group;
17721773
alignTo(container: Phaser.Rectangle | Phaser.Sprite | Phaser.Image | Phaser.Text | Phaser.BitmapText | Phaser.Button | Phaser.Graphics | Phaser.TileSprite, position?: number, offsetX?: number, offsetY?: number): Phaser.Group;
17731774
bringToTop(child: any): any;

0 commit comments

Comments
 (0)