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.
[cc lang=”actionscript3″ nowrap=”true” width=”550″]
/**
* 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= 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