Library for Tile-based games

function drawMap(map) {
    mapH = map.length;
    mapW = map[0].length;
    num = 0;
    attachMovie("tile", "tile", mapH * mapW);
    tileW = tile._width;
    tileH = tile._height;
    for (i = 0; i < mapW; i++) {
        for (j = 0; j < mapH; j++) {
            tile.duplicateMovieClip("tile" + num, num);
            t = this["tile" + num];
            t.gotoAndStop(map[j]*);
            //trace(map*[j]);
            t._width = tileW;
            t._height = tileH;
            t._x = tileW * i;
            t._y = tileH * j;
            num++;
            //trace(num);
        }
    }
    tile.removeMovieClip();
}
function checkTile(x, y) {
    count = mapH*mapW;
    for (k=0; k<count; k++) {
        name = _root["tile"+k];
        if (name.hitTest(x,y)) {
            return k;
        }
    }
}
MovieClip.prototype.nextTile = function() {
	if (this._currentframe == this._totalframes) this.gotoAndStop(1);
	else this.nextFrame();
};

It requires a movieclip in the library with a linkage name of tile. Inside this movieclip starting a frame 1, on each frame, is a tile (say grass on frame 1, water on frame 2, etc.).

Drawmap, obviously, draw’s the map. It’s paramater (map) is a 2d array that contains all the tiles needed. It also uses the only movieclip needed (at the moment!) which contains all our tiles.

Checktile, takes in two coordinates (x, y) and runs through all the tiles to see which one (if any) the coords are on. It returns the number of the tile. Drawmap labels each tile tile0, tile1, tile2, tile3, etc. and checktile returns the number in that name.

nextTile, is a prototype that is called on the tile movieclip, it’s basically an extended nextFrame() as it checks to see if it’s on the last frame and if so start again. This function has a use in a map editor.
An example using this and checkTile is, again, in a map editor, to change the selected tile to it’s next state.

_root["tile"+checkTile(_xmouse, _ymouse)].nextTile(); 

2d Array, is arrays inside arrays, used for building tile-worlds. Here is an example for a 10x10 world:

var map:Array = [ [3, 2, 2, 2, 2, 2, 2, 3, 1, 1], 
				  [3, 3, 3, 2, 2, 2, 3, 3, 1, 1], 
				  [1, 1, 3, 3, 3, 3, 3, 1, 1, 1], 
				  [1, 1, 1, 1, 1, 1, 4, 4, 4, 4], 
				  [1, 4, 4, 4, 4, 4, 4, 4, 4, 4],
				  [4, 4, 4, 4, 2, 2, 4, 4, 4, 4],
				  [4, 4, 4, 4, 2, 2, 4, 4, 4, 4],
				  [4, 4, 4, 4, 4, 2, 4, 4, 4, 4],
				  [1, 1, 1, 4, 4, 2, 4, 4, 1, 1],
				  [1, 1, 1, 1, 3, 2, 3, 1, 1, 1]
				];

Very small at the moment, but it’ll grow as I continue with my latest project. It’s the first time I’ve played with tiles.