Вы действительно не объяснили, как выглядит игровая логика.Но я подозреваю, что, возможно, все, что вам нужно сделать, это переместить игровую логику, которая наносит ущерб выше уровня рендеринга, а не после.
Редактировать: Как упоминалось в комментариях, если жизнь игрока может стать отрицательной, это также может привести к изображению, которое вы показываете, в этом случае вы можете добавить Math.max (значение, 0) кrender
// ANIMATION LOOP
function animate(currentTime) {
const animation = requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
// Move game logic that decreases player life to here.
// PLAYER LIFE
// RED REC
c.beginPath();
c.rect(20, 20, 150, 30);
c.fillStyle = "red";
c.fillRect(10, 30, 200, 20);
// YELLOW REC
c.beginPath();
c.rect(20, 20, 150, 30);
c.fillStyle = "yellow";
c.fillRect(10, 30, Math.max(player.life,0) , 20); // prevent negative value
// Is game logic that decreases player life here?
// END GAME
//I need some delay here so that fillRect has time to decrease the yellow bar to 0;
if (player.life <= 0) {
console.log("You Lose");
music.pause();
paused = true;
cancelAnimationFrame(animation);
}
}
animate();
Обычно вы бы организовали свой код примерно так.
var paused=false;
// Game Logic
function update()
{
enemyArray.forEach(function(enemy) {
if (collides(player, enemy)) {
player.life += -10;
enemy.delete();
}
});
if (player.life <= 0) {
console.log("You Lose");
music.pause();
paused = true;
}
}
// Render
function draw()
{
c.clearRect(0, 0, canvas.width, canvas.height);
// PLAYER LIFE
// RED REC
c.beginPath();
c.rect(20, 20, 150, 30);
c.fillStyle = "red";
c.fillRect(10, 30, 200, 20);
// YELLOW REC
c.beginPath();
c.rect(20, 20, 150, 30);
c.fillStyle = "yellow";
c.fillRect(10, 30, Math.max(player.life,0) , 20); // prevent negative value
}
// Animation Loop
function animate(currentTime) {
update();
draw();
if (!paused)
const animation = requestAnimationFrame(animate);
}
animate();