function update(){ if(left) x -= 5; if(right) x += 5; velocityY += gravity; y += velocityY; onGround = false; const platforms = document.querySelectorAll(".platform"); platforms.forEach(platform => { const px = platform.offsetLeft; const py = platform.offsetTop; const pw = platform.offsetWidth; const ph = platform.offsetHeight; if( x + 40 > px && x < px + pw && y + 40 >= py && y + 40 <= py + 20 && velocityY >= 0 ){ y = py - 40; velocityY = 0; onGround = true; } }); if(x < 0) x = 0; if(x > window.innerWidth - 40) x = window.innerWidth - 40; player.style.left = x + "px"; player.style.top = y + "px"; requestAnimationFrame(update); }