Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Knightfall game, Actionscript 3, Flash and Game development.

I was playing an old game called Knightfall when something came to my mind… ok, you can rotate the Display Object and this is easy, but how can you rotate the two dimensional array which is obviously storing game field information?

I mean some function which will turn an array like this:

[a][b][d]
[e][f][g][h]
[i][j][k][l]
[m][n][o][p]

into this one:

[m][i][e][a]
[n][j][f][b]
[o][k][g]
[p][l][h][d]

It seems an easy task but it’s not… and around the web you can only find examples building a new array, but I wanted to transform the array itself.

So I am sharing you these two functions to rotate by 90 degrees clockwise and counter clockwise any two dimensional array with the same number of rows and columns.

Enjoy it, as you won’t find a lot about this topic around the web.

package {
	import flash.display.Sprite;
	public class Main extends Sprite {
		public function Main() {
			var myArray:Array=[["a","b","c","d"],["e","f","g","h"],["i","j","k","l"],["m","n","o","p"]];
			traceArray(myArray);
			rotateClockwise(myArray);
			traceArray(myArray);
			rotateCounterClockwise(myArray);
			traceArray(myArray);
		}
		private function traceArray(a:Array) {
			for (var i:int=0; i

And this is the result: first, the original array, then the same array rotated clockwise, and last the rotated array rotated again, counter clockwise, to give us the original array.

a b c d 
e f g h 
i j k l 
m n o p 
-------
d h l p 
c g k o 
b f j n 
a e i m 
-------
a b c d 
e f g h 
i j k l 
m n o p 
-------

Hope you will find it useful, what about a rotating Bejeweled game?

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.