我无法从下面给出的对象“tilemap”中使用的匿名函数中检索结果。我相信onload函数会创建某种异步回调,但我不知道如何解决它。
提前致谢...
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;
})();
}