Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- window.addEventListener('load',Initieer);
- // Global variabels
- var image_tracker = 'white';
- var throws = [];
- function Initieer(){
- var btn = document.querySelector('.btn-yellow');
- document.querySelector('.btn-yellow').addEventListener('click', createRandomDices);
- document.querySelector('.dice').addEventListener("click",holdDice,false);
- console.log("The holdDice function has been called");
- }
- // Function to change the color of the dice whenever it is selected.
- // A tracker to keep track of the color it has, so you can hold(red) / roll(white) the dice.
- function holdDice(){
- console.log("used the function holdDice");
- Array.from(document.querySelectorAll(".dice")).forEach(d=>{
- d.onclick = function(){
- console.log("clicked:", this);
- this.classList.toggle("locked");
- var srcImage = this.src;
- console.log("The image src of this dice is " + srcImage);
- if(this.src.split("_red.png").length > 1){
- // assume it was red
- this.src = this.src.replace("_red.png",".png");
- } else {
- // assume it was white
- this.src = this.src.replace(".png","_red.png");
- }
- }
- })
- }
- function createRandomDices(){
- const diceElements = Array.from(document.querySelectorAll('.dice'));
- // Generate random numbers
- const dice = [
- {randomNumber: Math.floor(Math.random() * 6) + 1},
- {randomNumber: Math.floor(Math.random() * 6) + 1},
- {randomNumber: Math.floor(Math.random() * 6) + 1},
- {randomNumber: Math.floor(Math.random() * 6) + 1},
- {randomNumber: Math.floor(Math.random() * 6) + 1}
- ];
- for(let i in dice) {
- dice[i].image = 'img/dice' + dice[i].randomNumber + '.png';
- }
- //Display the dices on the page
- document.getElementById('dice-1').src = 'img/dice' + dice[0].randomNumber + '.png';
- document.getElementById('dice-2').src = 'img/dice' + dice[1].randomNumber + '.png';
- document.getElementById('dice-3').src = 'img/dice' + dice[2].randomNumber + '.png';
- document.getElementById('dice-4').src = 'img/dice' + dice[3].randomNumber + '.png';
- document.getElementById('dice-5').src = 'img/dice' + dice[4].randomNumber + '.png';
- }
Advertisement
Add Comment
Please, Sign In to add comment