mardi 29 mars 2016

Display random images if they have a different attribute

I have an array of 5 dice images that when I click a button, five random dice images show.

var diceImages = [ "img/dice-one.png","img/dice-two.png","img/dice-three.png", "img/dice-four.png", "img/dice-five.png", "img/dice-six.png"];

When I select some of those dice, they should not re-shuffle when I click that button again, and the unselected dice (containing data-selection = false) should reshuffle randomly again. The unselected dice are shuffling, but the problem is they're changing to the same image each time, they are not getting randomized. Here is a chunk of my code that's causing the problem:

  var rolls = [];

  for (var i = 0; i < 5; i++) {
  var randomNumber = Math.floor(Math.random() * diceImages.length);
  rolls.push(randomNumber + 1);
  var rollTheDice = $("[data-selection=false]"); //naming the unselected dice
  rollTheDice.each(function() { //Trying to display new random dice if data-selection = false
    $(this).attr('src', '' + diceImages[randomNumber]);
  });
  rollTheDice.attr('data',(randomNumber+1));
};




Aucun commentaire:

Enregistrer un commentaire