OPTIMIZE THIS! (If you can) ;)

Creating a tile game that’s loading maps from an ASP file defining where the tiles is as arrays, ultimately creating a matrix! :slight_smile: The asp loads the arrays in under 1 second, flash though takes about 20 seconds to load the tiles and freezes up while doing so aswell :frowning: Help please? And can I create a preloader telling how many tiles has been loaded?

stop();
import mx.transitions.Tween;
import mx.transitions.easing.*;
var suffixVar:String = "";//Defines the x&y coordinates when fetching a new map
var startX:Number = 0;
var startY:Number = 0;
var sessionID:Number = 0;
var loadSize:Number = 0;
_global.fullMapSize = 0;
_root.createEmptyMovieClip("tileMc",_root.getNextHighestDepth());//Holds the tiles :)

function removeUsAll() {//Used when reloading the map with new startX&Y
    _root.toggleAll(0);
    delete arrObj;
    delete loadTiles;
    for (var m = 0; m<=_global.finalK; m++) {
        removeMovieClip(this["tile"+m]);
        delete this["tile"+m];
    }
    _global.finalK = 0;
}

function loadAll(startX, startY, ID) {//startX=0, startY=0, ID=14
    this.tileHolder._visible = true;//Holds the tiles!
    var arrObj:Object = new Object();//Holds the arrays
    var sessionID:Number = Math.random()*Math.random();//To prevent cache
    var loadTiles:LoadVars = new LoadVars();
    loadTiles.ignoreWhite = true;//Do i need this? :(
    var suffixVar:String = "?x="+startX+"&y="+startY+"&id="+ID+"&sessionID="+sessionID;
    loadTiles.load("http://www.myniceurl.com/flashymap/map_tile.asp"+suffixVar);
    loadTiles.onLoad = function(success) {
        if (success) {
            var finito:Number = this.j;//Grabs how many rows is loaded, also defines the number of tiles load vertically!
            _global.dragSize = (finito*50)/2;//Don't mind this one
            for (var l = 0; l<=finito; l++) {
                arrObj["arr"+l] = this["tileArray"+l].split("|");//Splits the loaded variables into arrays (this is a quick process)
            }
            updateSQL(startX,startY,arrObj,finito);//THIS IS WHAT'S SLOW
        } else {
            trace("Failure to load tile arrays");
        }
    };
}

function updateSQL(startX, startY, arrObj, finito) {
    k = 0;//The tile's number
    for (var j = 0; j<=finito; j++) {
        var currArr:Number = Number(arrObj["arr"+j].length)-2;//The number of horizontal tiles, always equal to the vertical number (finito)
        for (var i = 0; i<=currArr; i++) {
            xy = arrObj["arr"+j]*;//grabs the number from the array, defining what mc-image to load from the library
            if (xy == "0") {//RANDOM TILE of desert
                var whatClip:String = "mc"+(Math.round(4*Math.random()+14));
                if (whatClip == "mc14") {//Correct random probability
                    whatClip = "mc18";
                }
            } else {
                whatClip = "mc"+xy;//Useless line right?
            }
            tileHolder.duplicateMovieClip("tile"+k,this.getNextHighestDepth());
            var _loc:MovieClip = this["tile"+k];
            _loc.attachMovie(whatClip,"clip"+k,this.getNextHighestDepth());
            _loc._x = -25-_global.dragSize+(50*i);//Places the tile
            _loc._y = -25-_global.dragSize+(50*j);
            _loc.bx = (startX-(finito/2))+i;//Gives it the correct X coord
            _loc.by = ((finito/2)+startY)-j;//And Y
            _loc.ttype = xy;//Saves a var telling what type of tile it is
            if (j == 0 || j == finito || i == 0 || i == currArr) {//Finds out if the tile is located on the edge of the map
                _loc["clip"+k]._alpha = 50;
                _loc.reloadBool = true;
            } else {
                _loc.reloadBool = false;
            }
            //this["tile"+k].cacheAsBitmap = true;
            _global.finalK = k;
            k++;
        }
    }
    _root.mapMc._x = 290;//Positions the map
    _root.mapMc._y = 290;
    if (_global.fullMapSize != 0) {
        if (_root.zoomHandle._y == 150) {//Fixes the Zoom, don't mind this
            _root.mapMc._width = _root.mapMc._height=_global.fullMapSize;
            _root.zoomHandle._y = 150;
            _root.mapMc.textBlob._width = _global.blobWidth;
            _root.mapMc.textBlob._height = _global.blobHeight;
        } else if (_root.zoomHandle._y == 200) {
            _root.mapMc._width = _root.mapMc._height=_global.fullMapSize/2;
            _root.zoomHandle._y = 200;
            _root.mapMc.textBlob._width = _global.blobWidth*2;
            _root.mapMc.textBlob._height = _global.blobHeight*2;
        } else if (_root.zoomHandle._y == 250) {
            _root.mapMc._width = _root.mapMc._height=_global.fullMapSize/3;
            _root.zoomHandle._y = 250;
            _root.mapMc.textBlob._width = _global.blobWidth*3;
            _root.mapMc.textBlob._height = _global.blobHeight*3;
        }
    } else {
        _global.fullMapSize = this._width;
    }
    _global.reloadMe = 0;
    this.tileHolder._visible = false;
    _root.toggleAll(1);
    var loadTween:Tween = new Tween(_root.loadMe, "_alpha", Regular.easeOut, _root.loadMe._alpha, 0, .5, true);
}

loadAll(Number(_global.startX),Number(_global.startY),Number(_global.ID));

_root.border.swapDepths(this.getNextHighestDepth());//Put the borders on top of the tiles
this.setMask(_root.mapMask);