Dice Roller Simulator
How does this work?
You can right-click and view the source on this to see exactly how it works. But here are the basics...
The super-simple explanation is that there is a timer running all the time. We use a couple of variables to determine if the dice are rolling or not. When they are done rolling, you get a final display. We also give the user a choice for how many dice to roll. So we have to allow for that too.
Set your variables, and use the setNumDice() function to respond when the user change the dropdown for HowManyDice.
When the user clicks the ROLL button, we call the animateDice() function which sets the flags to show the animation.
Here is the animation function - the timer. This is running all the time. But as long as the flag for animating is false, nothing happens. The timer fires once every 10 milliseconds. Once the animating variable is set to TRUE, the animCount variable counts up how many times it has fired.
Each time the timer fires, it loops through the available (active) dice, and chooses a new random dice image and displays it. It does this every 10 milliseconds. So the result is a flash of random images.
Once it has fired 50 times, the flag for animating is set to false and RollDice() is called.
This function resets the dice values, randomly chooses new values, and ads them up. The user sees the final result.