I am writing a tile based platformer in JavaScript. I was thinking about storing the level data in a JSON file and then using XMLHttpRequest to grab the file and then load it using XMLHttpRequest.onreadystatechange like this:
TileMap.prototype.loadFile = function(pathToFile) {
'use strict';
var xhr = new XMLHttpRequest();
var that = this;
xhr.onreadystatechange = function() {
if(xhr.readyState === 4 && xhr.status === 200) {
// parse json file and load tilemap stuff
}
};
PlayState.prototype.init = function() {
this.tileMap = new TileMap();
this.tileMap.loadFile('level-1.json');
this.camera = new Camera(0, 0, this.tileMap.mapLayers[0].width *
this.tileMap.mapLayers[0].tileWidth, canvasWidth,
this.tileMap.mapLayers[0].height *
this.tileMap.mapLayers[0].tileHeight, canvasHeight);
};
However, I'm not sure if this is the best way of going about this. Would other parts of my game be running while this is loading? Would it be better if I hardcoded the data into my game? What do you guys think?