I am currently making a game in HTML/JS that includes approximately 1200 blocks per level. All of the blocks are individual images, but they are a lot of the time similar. They are 20*20 pixels. After inserting the pictures instead of placeholder divs, the perfomance has gone down a lot.
I am not sure, if it is because of the bandwith, but I would expect the pc to cache the images and reuse it.
Or maybe it is a memory problem with the amount of images.
socket.on("sendBlocks",function(blocks,blocksCoords){
if(typeof blocksCoords[area.X + "_" + area.Y] !== "undefined"){
mapLimit.artX = 0;
mapLimit.artY = -1;
while(mapLimit.X + mapLimit.Y != mapLimit.artX + mapLimit.artY){
mapLimit.artY = mapLimit.artY + 1;
if(mapLimit.artY > mapLimit.Y){
mapLimit.artX = mapLimit.artX + 1;
mapLimit.artY = 0;
}
//Change block, executed for every art-coord.
if(typeof blocksCoords[area.X + "_" + area.Y][mapLimit.artX + "_" + mapLimit.artY] !== "undefined"){
switch(blocksCoords[area.X + "_" + area.Y][mapLimit.artX + "_" + mapLimit.artY].type){
case "wood":
$("#" + mapLimit.artX.toString() + "_" + mapLimit.artY.toString()).attr("src","https://db.tt/TyZBx7EG");
break;
case "empty":
$("#" + mapLimit.artX.toString() + "_" + mapLimit.artY.toString()).attr("src","https://db.tt/SdXqMMiE");
break;
}
}else if(typeof blocksCoords[area.X + "_" + area.Y][mapLimit.artX + "_" + mapLimit.artY] === "undefined"){
$("#" + mapLimit.artX.toString() + "_" + mapLimit.artY.toString()).attr("src","https://db.tt/SdXqMMiE");
}
}
}else if(typeof blocksCoords[area.X + "_" + area.Y] === "undefined"){
$(".block").css("background-color","white");
}
This code will be executed every time that the blocks are updated from the server. Checking if the block is wood, empty or undefined. Giving it different textures for each type of block.
The server updates the blocks every 100 ms, is that too fast?
Any suggestions to how this problem can be solved?
Thanks a lot :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…