I’m creating a HTML/JS browser based game, and I’m trying to have any element that has a higher Y on the page to have a lower Z Index, making it appear behind elements with a lower Y, the code I’ve written seems to be doing just that, by setting the Z Index to the elements top positioning, but visually it doesn’t
The code for placing elements:
async start(xx, yy, p, sxx, syy) {
document.getElementById("htmlIcon").href = `/media/misc/${p}/icon.png`;
new Promise(async (resolve) => {
const x = xx;
const y = yy || xx;
const sx = sxx || (x * 50) / 2;
const sy = syy || (y * 50) / 2;
this.mx = x;
this.my = y;
this.inventory.update();
this.map.style.width = `${x * 50}px`;
this.map.style.height = `${y * 50}px`;
this.map.style.overflow = "hidden";
document.body.style.width = `${x * 50}px`;
document.body.style.height = `${y * 50}px`;
this.map.innerHTML = "";
this.rawMap = [];
this.layerHandler();
for (let i = 0; i < y; i++) {
for (let j = 0; j < x; j++) {
// Grass
const tile = document.createElement("img");
tile.src =
this.data.tiles.grass.image[
Math.floor(Math.random() * this.data.tiles.grass.image.length)
];
tile.classList.add("tile");
tile.style.left = `${i * 50}px`;
tile.style.top = `${j * 50}px`;
tile.style.width = `${this.vars(this.data.tiles.grass.size)}px`;
tile.style.zIndex = 1;
this.map.appendChild(tile);
}
}
for (let i = 0; i < y; i++) {
let layer = "";
for (let j = 0; j < x; j++) {
const tile = Math.floor(Math.random() * 100);
if (tile <= 50) {
// Air
layer += "null/";
}
if (tile <= 75 && tile > 50) {
// Flowers
const div = document.createElement("img");
div.src =
this.data.tiles.flower.image[
Math.floor(Math.random() * this.data.tiles.flower.image.length)
];
div.classList.add("tile");
div.classList.add("breakable");
div.classList.add("layers");
const offsetX = this.vars(this.data.tiles.flower.offsetX) * 1;
const offsetY = this.vars(this.data.tiles.flower.offsetY) * 1;
div.style.left = `${i * 50 + offsetX}px`;
div.style.top = `${j * 50 + offsetY}px`;
div.style.width = `${this.vars(this.data.tiles.flower.size)}px`;
this.map.appendChild(div);
layer += "flower/";
} else if (tile <= 100 && tile > 75) {
// Trees
const div = document.createElement("img");
div.src =
this.data.tiles.tree.image[
Math.floor(Math.random() * this.data.tiles.tree.image.length)
];
div.classList.add("tile");
div.classList.add("breakable");
div.classList.add("layers");
const offsetX = this.vars(this.data.tiles.tree.offsetX) * 1;
const offsetY = this.vars(this.data.tiles.tree.offsetY) * 1;
div.style.left = `${i * 50 + offsetX}px`;
div.style.top = `${j * 50 + offsetY}px`;
div.style.width = `${this.vars(this.data.tiles.tree.size)}px`;
this.map.appendChild(div);
layer += "tree/";
}
}
this.rawMap.push(layer);
}
// Player
const entity = this.data.entities.player[p];
const player = document.createElement("img");
player.classList.add("entity");
player.classList.add("layers");
player.style.left = `${sx}px`;
player.style.top = `${sy}px`;
player.style.width = `${entity.size}px`;
player.id = `player`;
this.playerSprites = [];
// Get data URIs of each sprite
function uri(blob) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
}
await fetch(entity.image + "up.png")
.then((res) => res.blob())
.then(async (blob) => this.playerSprites.push(await uri(blob)));
await fetch(entity.image + "down.png")
.then((res) => res.blob())
.then(async (blob) => this.playerSprites.push(await uri(blob)));
await fetch(entity.image + "left.png")
.then((res) => res.blob())
.then(async (blob) => this.playerSprites.push(await uri(blob)));
await fetch(entity.image + "right.png")
.then((res) => res.blob())
.then(async (blob) => this.playerSprites.push(await uri(blob)));
player.src = this.playerSprites[1];
this.map.appendChild(player);
this.track(player);
this.movement(player, p);
resolve();
});
}
The code for handling layers:
layerHandler(type = ".layers") {
document.querySelectorAll(type).forEach((elm) => {
const rect = elm.getBoundingClientRect();
elm.style.zIndex = Math.floor(rect.top + window.scrollY);
});
}
This is also on GitHub here: https://github.com/QueenAka/barely-alive