I'm unable to retrieve results from the anonymous function used inside the object "tilemap" given below. I believe the onload function creates some sort of asynchronized callback but I'm not sure how could I fix it.
Thanks in advance...
function generateTileMap(imageLocation, w, h, name = "Tilemap") {
var image = new Image();
image.src= imageLocation;
return (image.onload = function () {
if (image.naturalWidth % w > 0) {
return false; //if width is out of scope
}
if (image.naturalHeight % h > 0) {
return false; //if height is out of scope
}
var tilemap = {
conf: {
name: name,
width: w,
height: h,
image: image
},
frames: (function () { //Unable to return frames. Async?
var frames = [];
for (var i = 0; i < image.naturalHeight / h; i++) {
for (var j = 0; j < image.naturalWidth / w; j++) {
frames.push({
x: j,
y: i
});
}
}
return frames;
})()
};
return tilemap;
})();
}