I'm new to programming and am designing a game for a school assignment. I have made a small animation in CSS of a dice being shaken, then displaying the number of eyes of the throw. What I want is to call on the function playYourTurn() once (linked to a button), and then it would run the animation twice with some delay inbetween and move the players accordingly.
As it is now, only the second animation, playSpino(), runs. I have tried many variations with not having them as separate functions, change the intervals etc but to no avail. Sometimes it's only the first animation, playSasha(), that runs. Borth markers move accordingly to their throws, I have checked by writing it out in the log, so it's just the animations I'm struggling with.
The functions and the function in which they're being called:
function playYourTurn()
{
playSasha();
window.setTimeout(() => { }, 1500);
playSpino();
}
function playSasha()
{
var diceResultSasha = rollDice();
window.setTimeout(() => { moveSasha(diceResultSasha) }, 1000)
window.setTimeout(resetDice, 3000)
}
function playSpino()
{
var diceResultSpino = rollDice();
window.setTimeout(() => { moveSpino(diceResultSpino) }, 4000)
window.setTimeout(resetDice, 7000)
}
The function with the animation that currently only runs once:
function rollDice()
{
resetDice();
var diceresult = document.getElementById('diceresult')
var dice = Math.floor(Math.random() * 6) + 1;
document.getElementById('shakebox').className = 'diceAnimation';
diceresult.className = 'resultVisible';
(a switch statement follows here).
Here is the CSS code for the animation:
.diceAnimation
{
animation: shake 0.2s;
animation-iteration-count: 5;
}
@keyframes shake
{
0% { transform: translate(1px, 1px) rotate(0deg); }
10% { transform: translate(-2px, -3px) rotate(-20deg); }
20% { transform: translate(-4px, 0px) rotate(20deg); }
30% { transform: translate(3px, 2px) rotate(0deg); }
40% { transform: translate(1px, -1px) rotate(10deg); }
50% { transform: translate(-1px, 2px) rotate(-10deg); }
60% { transform: translate(-3px, 1px) rotate(0deg); }
70% { transform: translate(3px, 1px) rotate(-20deg); }
80% { transform: translate(-1px, -1px) rotate(20deg); }
90% { transform: translate(1px, 2px) rotate(0deg); }
100% { transform: translate(1px, -2px) rotate(-10deg); }
}
There is also a CSS effect that makes the default dice fade in to the number of eyes thrown that looks like this:
#diceresult
{
opacity: 0;
position: absolute;
}
.resultVisible
{
opacity: 1 !important;
transition: all 1s ease;
}
dicewith regards to the animation?