Here’s an Actionscript 3 class I wrote the places tiles on a grid – either randomly or in order, with no overlap, and allows for a tile free zone.
You can see it in action here.
/**
* coding by mike Connor - flashDeveloper@rocketnumber9.org
*
* randomly places tiles on a grid so that o not overlap and do not fall in dead tile zone
*/
package utils
{
import flash.display.DisplayObject;
public class RandomTile
{
private var numOfRows:uint;
private var numOfCols:uint;
private var rowArray:Array;
private var tileArray:Array;
private var unplacedArray:Array = new Array();
private var _gridW:uint;
private var _gridH:uint;
private var _maginTop:uint;
private var _marginLeft:uint;
private var _deadTiles:Array = new Array();
private var _randomLayout:Boolean;
public function RandomTile(r:uint, col:uint, ta:Array, gridW:uint = 76, gridH:uint = 76,
maginTop:uint = 2, marginLeft:uint=1, randomLayout:Boolean= true, deadTile:Array = null)
{
rowArray = new Array();
_randomLayout = randomLayout;
numOfRows = r;
numOfCols = col;
tileArray = ta;
_gridW = gridW;
_gridH = gridH;
_maginTop =maginTop;
_marginLeft = marginLeft;
if (deadTile !=null) _deadTiles = deadTile;
if (numOfRows * numOfCols < (tileArray.length - _deadTiles.length)) {
throw new Error("Error - too many tiles, not enough spaces");
} else {
init();
}
}
private function init():void {
for (var i:uint=0; i<numOfRows; i++){
var colArray:Array = new Array();
for (var j:uint=0; j<numOfCols; j++){
colArray.push(j);
}
//trace(" row " + i + " has " + colArray.length + " columns");
rowArray.push(colArray);
}
removeOfflimitesTiles();
if (_randomLayout){
placeTilesRandomly(tileArray);
} else {
placeTilesInOrder(tileArray);
}
}
private function removeOfflimitesTiles():void {
for (var k:uint = 0; k< _deadTiles.length; k++) {
//trace("dead row " + _deadTiles[k].row + " dead col " + _deadTiles[k].col);
rowArray[_deadTiles[k].row][_deadTiles[k].col] = undefined;
}
}
/* private function simplyPlaceTiles(arr:Array):void {
for (var r:uint = 0; r < numOfRows; r++){
for (var c:uint = 0; c < numOfCols; c++){
for (var i:uint = 0; i<arr.length; i++){
DisplayObject(arr[i]).x = _gridW* c) + _marginLeft;
DisplayObject(arr[i]).y = _gridH* r) + _maginTop;
}
}
}
}
*/
private function placeTilesInOrder(arr:Array):void {
var rowCount:uint = 0;
var colCount:uint = 0;
for (var i:uint = 0; i < arr.length; i++){
var colArray:Array =rowArray[rowCount] as Array;
var numColsinThisRow:uint = colArray.length;
if (rowArray[rowCount][colCount] != undefined) {
DisplayObject(arr[i]).x = (_gridW* colCount) + _marginLeft;
DisplayObject(arr[i]).y = (_gridH* rowCount) + _maginTop;
Debug.trace_msg(" placeTiles in r:" + rowCount + ", col:" + colCount, Debug.DEBUG_VERBOSE);
rowArray[rowCount][colCount] = undefined;
} else {
Debug.trace_msg("trace", Debug.DEBUG_VERBOSE);
arr.splice(0, i-1);
i= 0;
}
colCount++;
if (colCount >= numOfCols){
colCount = 0;
rowCount++;
}
}
Debug.trace_msg("DONE placeTiles in order", Debug.DEBUG_VERBOSE);
}
private function placeTilesRandomly(arr:Array):void {
for (var i:uint = 0; i<arr.length; i++){
var randomRow:uint = Math.floor(Math.random()*numOfRows);
var colArray:Array = rowArray[randomRow] as Array;
var numColsinThisRow:uint = colArray.length;
var randomColinRow:uint = Math.floor(Math.random()*rowArray[randomRow].length);
if (rowArray[randomRow][randomColinRow] != undefined) {
DisplayObject(arr[i]).x = (_gridW* randomColinRow) + _marginLeft;
DisplayObject(arr[i]).y = (_gridH* randomRow) + _maginTop;
Debug.trace_msg(" placeTiles in r:" + randomRow + ", col:" + randomColinRow, Debug.DEBUG_VERBOSE);
rowArray[randomRow][randomColinRow] = undefined;
} else {
Debug.trace_msg("trace", Debug.DEBUG_VERBOSE);
arr.splice(0, i-1);
i= 0;
}
}
Debug.trace_msg("DONE placeTiles", Debug.DEBUG_VERBOSE);
}
}
}
* coding by mike Connor - flashDeveloper@rocketnumber9.org
*
* randomly places tiles on a grid so that o not overlap and do not fall in dead tile zone
*/
package utils
{
import flash.display.DisplayObject;
public class RandomTile
{
private var numOfRows:uint;
private var numOfCols:uint;
private var rowArray:Array;
private var tileArray:Array;
private var unplacedArray:Array = new Array();
private var _gridW:uint;
private var _gridH:uint;
private var _maginTop:uint;
private var _marginLeft:uint;
private var _deadTiles:Array = new Array();
private var _randomLayout:Boolean;
public function RandomTile(r:uint, col:uint, ta:Array, gridW:uint = 76, gridH:uint = 76,
maginTop:uint = 2, marginLeft:uint=1, randomLayout:Boolean= true, deadTile:Array = null)
{
rowArray = new Array();
_randomLayout = randomLayout;
numOfRows = r;
numOfCols = col;
tileArray = ta;
_gridW = gridW;
_gridH = gridH;
_maginTop =maginTop;
_marginLeft = marginLeft;
if (deadTile !=null) _deadTiles = deadTile;
if (numOfRows * numOfCols < (tileArray.length - _deadTiles.length)) {
throw new Error("Error - too many tiles, not enough spaces");
} else {
init();
}
}
private function init():void {
for (var i:uint=0; i<numOfRows; i++){
var colArray:Array = new Array();
for (var j:uint=0; j<numOfCols; j++){
colArray.push(j);
}
//trace(" row " + i + " has " + colArray.length + " columns");
rowArray.push(colArray);
}
removeOfflimitesTiles();
if (_randomLayout){
placeTilesRandomly(tileArray);
} else {
placeTilesInOrder(tileArray);
}
}
private function removeOfflimitesTiles():void {
for (var k:uint = 0; k< _deadTiles.length; k++) {
//trace("dead row " + _deadTiles[k].row + " dead col " + _deadTiles[k].col);
rowArray[_deadTiles[k].row][_deadTiles[k].col] = undefined;
}
}
/* private function simplyPlaceTiles(arr:Array):void {
for (var r:uint = 0; r < numOfRows; r++){
for (var c:uint = 0; c < numOfCols; c++){
for (var i:uint = 0; i<arr.length; i++){
DisplayObject(arr[i]).x = _gridW* c) + _marginLeft;
DisplayObject(arr[i]).y = _gridH* r) + _maginTop;
}
}
}
}
*/
private function placeTilesInOrder(arr:Array):void {
var rowCount:uint = 0;
var colCount:uint = 0;
for (var i:uint = 0; i < arr.length; i++){
var colArray:Array =rowArray[rowCount] as Array;
var numColsinThisRow:uint = colArray.length;
if (rowArray[rowCount][colCount] != undefined) {
DisplayObject(arr[i]).x = (_gridW* colCount) + _marginLeft;
DisplayObject(arr[i]).y = (_gridH* rowCount) + _maginTop;
Debug.trace_msg(" placeTiles in r:" + rowCount + ", col:" + colCount, Debug.DEBUG_VERBOSE);
rowArray[rowCount][colCount] = undefined;
} else {
Debug.trace_msg("trace", Debug.DEBUG_VERBOSE);
arr.splice(0, i-1);
i= 0;
}
colCount++;
if (colCount >= numOfCols){
colCount = 0;
rowCount++;
}
}
Debug.trace_msg("DONE placeTiles in order", Debug.DEBUG_VERBOSE);
}
private function placeTilesRandomly(arr:Array):void {
for (var i:uint = 0; i<arr.length; i++){
var randomRow:uint = Math.floor(Math.random()*numOfRows);
var colArray:Array = rowArray[randomRow] as Array;
var numColsinThisRow:uint = colArray.length;
var randomColinRow:uint = Math.floor(Math.random()*rowArray[randomRow].length);
if (rowArray[randomRow][randomColinRow] != undefined) {
DisplayObject(arr[i]).x = (_gridW* randomColinRow) + _marginLeft;
DisplayObject(arr[i]).y = (_gridH* randomRow) + _maginTop;
Debug.trace_msg(" placeTiles in r:" + randomRow + ", col:" + randomColinRow, Debug.DEBUG_VERBOSE);
rowArray[randomRow][randomColinRow] = undefined;
} else {
Debug.trace_msg("trace", Debug.DEBUG_VERBOSE);
arr.splice(0, i-1);
i= 0;
}
}
Debug.trace_msg("DONE placeTiles", Debug.DEBUG_VERBOSE);
}
}
}
Rocket 9 is the personal and professional online presence of Flash developer and citizen Mike Connor.
Cloud Swing, Inc. is our software development shop.
One Trackback
[...] is a very simple demo of game coding skills. I used my RandomTile Class that I wrote in 2009. This class places tiles on a grid randomly (or not) and allows you to [...]