Basically what I have been trying to do, is make a loop that will cycle through a tonne(only 36 atm) of tiles, and place them in order. While that part works, it doesn't actually load the images and display the images.
Using console.log, I've narrowed the problem down to when it draws the image(or at least I'm pretty sure that's the problem).
I've tried switching local, and global variables, and that doesn't seem to do anything, and I've also switched what I had as just:
ctx.drawImage(loadTile, tileToLoadX, tileToLoadY);
To:
loadTile.onload = function() {
ctx.drawImage(loadTile, tileToLoadX, tileToLoadY);
};"
As many people have suggested to do with similar problems.
var canvas = document.getElementById("mapcanvas");
var ctx = canvas.getContext("2d");
GTH = 3000;
GTW = 3000;
tileCountX = 6;
tileCountY = 6;
mapWidth = tileCountX * GTW;
mapHeight = tileCountY * GTH;
function imageSearch() {
for (u=1; u < tileCountY + 1; u++) {
for (i=1; i < tileCountX + 1;i++) {
tileToLoad = "x" + i + "y" + u;
tileToLoadX = i * GTW - GTW;
tileToLoadY = u * GTH - GTH;
console.log("Successfully found tile " + tileToLoad);
loadImg();
}
}
};
function loadImg() {
var loadTile = new Image();
loadTile.src = 'Top/x+y+/' + tileToLoad + ".png";
loadTile.onload = function() {
ctx.drawImage(loadTile, tileToLoadX, tileToLoadY);
};
console.log("Successfully loaded tile " + tileToLoad);
};
imageSearch();
Also note that the canvas is supposed to be the same size as the tiles, and that GTH, and GTW are the default heights and widths of all the tiles.
Help would be appreciated! Thank you!
In your function
The variables
tileToLoad
,tileToLoadX
andtileToLoadY
belong to the functionimageSearch()
as local variables and would be undefined.Also, the
loadTile.onload
action need to be set BEFORE you assign thesrc
for the image variable.Try making the following changes.