My first game involves triggering an animation that transitions the game screen to greyscale when the character dies. Despite my efforts, I have been unable to successfully trigger this animation using
document.getElementById("object").animationPlayState = "running"
. The CSS code for the game is enclosed within a <div>
element in HTML.
To view the full code on Codepen (which currently has some issues), please click on this link: https://codepen.io/flyingchicken22/pen/NWgVrJq
CSS:
#game {
width: 600px;
height: 300px;
border: 2px solid black;
margin: auto;
background-image: url("https://t3.ftcdn.net/jpg/01/73/79/26/360_F_173792623_516juhwjkCCZJ9OyesyH7hf7r9zsyH5u.jpg");
background-size: cover;
animation: deathAnimation 2s;
animation-play-state: paused;
}
@keyframes deathAnimation {
0%{filter:none;}
100%{filter:grayscale(100%);}
}
Javascript:
var gameLoop = setInterval(() => {
dead = false
function rockAnimation() {
rock.classList.add('rockAnimation')
}
const knightTop = parseInt(window.getComputedStyle(knight).getPropertyValue('top'))
const rockLeft = parseInt(window.getComputedStyle(rock).getPropertyValue('left'))
if (rockLeft < 0) {
rock.style.display = 'none'
} else {
rock.style.display = ''
}
if (rockLeft < 90 && rockLeft > 50 && knightTop > 79) {
dead = true
}
score.innerText++
const deathAnimation = document.getElementById("deathAnimation")
const game = document.getElementById("game")
if (dead == true) {
clearInterval(gameLoop)
document.getElementById("youDied").innerHTML = "YOU DIED"
document.getElementById("playAgain").innerHTML = "Play Again?"
game.classList.add(deathAnimation)
document.getElementById("deathAnimation").style.animationPlayState = "running"
}
}, 50);