Skip to content

Commit c32706f

Browse files
committed
Phaser.Utils.transposeArray will transpose the given array and return it.
Phaser.Utils.rotateArray will rotate the given array by 90 or 180 degrees in either direction and return it.
1 parent 5eb7ae2 commit c32706f

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Version 2.0.5 - "Tanchico" - in development
7979
* InputManager.minPriorityID lets you set the minimum priority level an object needs to be to be checked by a Pointer. Useful for UI layer stacking.
8080
* New consts: Phaser.Tilemap.NORTH, SOUTH, EAST and WEST to use with the TileMapWalker Plugin.
8181
* BitmapData.processPixelRGB added undefined check (thanks @muclemente, fix #808)
82+
* Phaser.Utils.transposeArray will transpose the given array and return it.
83+
* Phaser.Utils.rotateArray will rotate the given array by 90 or 180 degrees in either direction and return it.
8284

8385

8486
### New Plugins

src/utils/Utils.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,71 @@
1212
*/
1313
Phaser.Utils = {
1414

15+
/**
16+
* Transposes the elements of the given Array.
17+
*
18+
* @method transposeArray
19+
* @param {array} array - The array to transpose.
20+
* @return {array} The transposed array.
21+
*/
22+
transposeArray: function (array) {
23+
24+
var result = new Array(array[0].length);
25+
26+
for (var i = 0; i < array[0].length; i++)
27+
{
28+
result[i] = new Array(array.length - 1);
29+
30+
for (var j = array.length - 1; j > -1; j--)
31+
{
32+
result[i][j] = array[j][i];
33+
}
34+
}
35+
36+
return result;
37+
38+
},
39+
40+
/**
41+
* Rotates the given array.
42+
* Based on the routine from http://jsfiddle.net/MrPolywhirl/NH42z/
43+
*
44+
* @method Phaser.Utils.rotateArray
45+
* @param {array} matrix - The array to rotate.
46+
* @param {number|string} direction - The amount to rotate. Either a number: 90, -90, 270, -270, 180 or a string: 'rotateLeft', 'rotateRight' or 'rotate180'
47+
* @return {array} The rotated array
48+
*/
49+
rotateArray: function (matrix, direction) {
50+
51+
if (typeof direction !== 'String')
52+
{
53+
direction = ((direction % 360) + 360) % 360;
54+
}
55+
56+
if (direction === 90 || direction === -270 || direction === 'rotateLeft')
57+
{
58+
matrix = Phaser.Utils.transposeArray(matrix);
59+
matrix = matrix.reverse();
60+
}
61+
else if (direction === -90 || direction === 270 || direction === 'rotateRight')
62+
{
63+
matrix = matrix.reverse();
64+
matrix = Phaser.Utils.transposeArray(matrix);
65+
}
66+
else if (Math.abs(direction) === 180 || direction === 'rotate180')
67+
{
68+
for (var i = 0; i < matrix.length; i++)
69+
{
70+
matrix[i].reverse();
71+
}
72+
73+
matrix = matrix.reverse();
74+
}
75+
76+
return matrix;
77+
78+
},
79+
1580
/**
1681
* Get a unit dimension from a string.
1782
*

0 commit comments

Comments
 (0)