2016-08-19 10 views
1

私は現在ダイスローラーを作成中です。私が解決しようとしている私の問題は、私自身のサイコロが画像要素であるということです。私は、サイコロのサイコロの数値を示すメッセージをアラートに表示するために数字を割り当てたいと思っています。たとえば、最初のサイコロの値が3の場合、「スコアをチェック」ボタンをクリックすると、最初のメッセージに「3」と表示されます。私の問題は、警告が[object HTMLImageElement]を表示することです。ダイスローラーゲームでサイコロに番号を割り当てる

私のコードを見ればもっと良いことが分かるはずです。 私のフルコードはこの最初のカットの下にあります。参考のためだけであり、私が修正すべき特定の領域は後になります。


<!doctype html> 

<title>Yahtzee Dice Roller</title> 
<link rel="stylesheet" href="styles.css"> 

<!-- HIDDEN HTML IMAGES USED TO STORE THE SIX DIE ROLL IMAGES --> 
<img id="srcImage1" class="srcImageClass" src="images/die1.jpg"> 
<img id="srcImage2" class="srcImageClass" src="images/die2.jpg"> 
<img id="srcImage3" class="srcImageClass" src="images/die3.jpg"> 
<img id="srcImage4" class="srcImageClass" src="images/die4.jpg"> 
<img id="srcImage5" class="srcImageClass" src="images/die5.jpg"> 
<img id="srcImage6" class="srcImageClass" src="images/die6.jpg"> 

<!-- THIS IS THE GAME BOARD AS SEEN ON THE BROWSER --> 
<div id="stage"> 

    <img id="gameDie1" class="gameDieClass" src="images/die1.jpg"> 
    <img id="gameDie2" class="gameDieClass" src="images/die1.jpg"> 
    <img id="gameDie3" class="gameDieClass" src="images/die1.jpg"> 
    <img id="gameDie4" class="gameDieClass" src="images/die1.jpg"> 
    <img id="gameDie5" class="gameDieClass" src="images/die1.jpg"> 

    <span id="spanDie1" class="spanDieClass">HOLD</span> 
    <span id="spanDie2" class="spanDieClass">HOLD</span> 
    <span id="spanDie3" class="spanDieClass">HOLD</span> 
    <span id="spanDie4" class="spanDieClass">HOLD</span> 
    <span id="spanDie5" class="spanDieClass">HOLD</span> 

    <button id="rollButton">Roll Dice</button> 

    <button id = "reset">Reset Dice</button> 

    <button id = "score">Check Score</button> 

</div> 

<script> 

// *********************************************************** 
// GLOBAL VARIABLES TO BE USED THROUGHOUT THE GAME 
// *********************************************************** 

// declare an array variable to store each of the html gameDie img tags 
var gameDie = []; 
gameDie[1] = document.querySelector("#gameDie1"); 
gameDie[2] = document.querySelector("#gameDie2"); 
gameDie[3] = document.querySelector("#gameDie3"); 
gameDie[4] = document.querySelector("#gameDie4"); 
gameDie[5] = document.querySelector("#gameDie5"); 

// declare an array variable to store each of the html span (freeze dice) tags 
var spanDie = []; 
spanDie[1] = document.querySelector("#spanDie1"); 
spanDie[2] = document.querySelector("#spanDie2"); 
spanDie[3] = document.querySelector("#spanDie3"); 
spanDie[4] = document.querySelector("#spanDie4"); 
spanDie[5] = document.querySelector("#spanDie5"); 



// declare an array to keep track of the "frozen" state of each die 
var gameDieHold = []; 
gameDieHold[1] = false; 
gameDieHold[2] = false; 
gameDieHold[3] = false; 
gameDieHold[4] = false; 
gameDieHold[5] = false; 



var rollNum = 0; 
var canRoll = true; 


gameDie[1].addEventListener("click", gameDieClick.bind(this,1), false); 
gameDie[2].addEventListener("click", gameDieClick.bind(this,2), false); 
gameDie[3].addEventListener("click", gameDieClick.bind(this,3), false); 
gameDie[4].addEventListener("click", gameDieClick.bind(this,4), false); 
gameDie[5].addEventListener("click", gameDieClick.bind(this,5), false); 


// declare an array variable to store each of the hidden html srcImage img tags 
var srcImage = []; 
srcImage[1] = document.querySelector("#srcImage1"); 
srcImage[2] = document.querySelector("#srcImage2"); 
srcImage[3] = document.querySelector("#srcImage3"); 
srcImage[4] = document.querySelector("#srcImage4"); 
srcImage[5] = document.querySelector("#srcImage5"); 
srcImage[6] = document.querySelector("#srcImage6"); 

// Grab the "roll dice" button and attach a "click" function to it 
var rollButton = document.querySelector("#rollButton"); 
rollButton.addEventListener("click", rollButtonClick, false); 

var reset = document.querySelector("#reset"); 
reset.addEventListener("click", resetButton, false); 

var score = document.querySelector("#score"); 
score.addEventListener("click", scoreButtonClick, false); 


renderGameDieSpans(); 

// *********************************************************** 
// FUNCTIONS USED BY THE GAME 
// *********************************************************** 

function gameDieClick(zNum) { 
    if (canRoll === true) { 
    gameDieHold[zNum] =! gameDieHold[zNum]; 
    renderGameDieSpans(); 
    } 
} 


function renderGameDieSpans() { 
    // loop through all of the dice, and only show the hold span if the die is set to HOLD 
    for (var i=1; i<=5; i++) { 
     if (gameDieHold[i]) { 
      spanDie[i].style.visibility = "visible"; 
     } else { 
      spanDie[i].style.visibility = "hidden"; 
     } 
    } 
} 

function randomNum(zMin, zMax) { 
    // This function generates, and returns, a random integer 
    // between zMin and zMax 
    return Math.floor(Math.random()*(zMax-zMin+1) + zMin); 
} 

function rollButtonClick() { 
    rollNum++; 
    // loop through each of the 5 game dice and set its "src" image 
    // to one of the 6 random hidden images 
    for(i=1; i<=5; i++) { 
     if (gameDieHold[i] === false) { 
     var tempNumber = randomNum(1,6); 
     gameDie[i].src = srcImage[tempNumber].src; 

     if (rollNum === 3) { 
      canRoll = false; 

      if (canRoll === false) { 
       console.log("working"); 
        gameDieHold[1] = true; 
        gameDieHold[2] = true; 
        gameDieHold[3] = true; 
        gameDieHold[4] = true; 
        gameDieHold[5] = true; 

        renderGameDieSpans(); 

        rollButton.removeEventListener("click", rollButtonClick, false); 
        rollButton.disabled = true; 


       } 
      } 

     } 
    }   
} 

//reset variables 
function resetButton() { 
    console.log("button working"); 
    rollNum = 0; 
    canRoll = true; 
    gameDieHold[1] = false; 
    gameDieHold[2] = false; 
    gameDieHold[3] = false; 
    gameDieHold[4] = false; 
    gameDieHold[5] = false; 

    rollButton.addEventListener("click", rollButtonClick, false); 
    rollButton.disabled = false; 



    renderGameDieSpans(); 


//below are if statements that check if various parts of the function is working  
    if (rollNum === 0) { 
     console.log("rollNum equal to 0"); 
    } 

    if (canRoll === true) { 
     console.log("canRoll is true"); 

     if (gameDieHold[1] === false) { 
      console.log("gameDieHold is false"); 
     } 
    } 

} 


function scoreButtonClick() { 
    var tempMsg = ""; 
    tempMsg = gameDie[1] + " "; 
    tempMsg += gameDie[2] + " "; 
    tempMsg += gameDie[3] + " "; 
    tempMsg += gameDie[4] + " "; 
    tempMsg += gameDie[5] + " "; 
    alert(tempMsg); 
} 

</script> 

特定の領域 -


function scoreButtonClick() { 
    var tempMsg = ""; 
    tempMsg = gameDie[1] + " "; 
    tempMsg += gameDie[2] + " "; 
    tempMsg += gameDie[3] + " "; 
    tempMsg += gameDie[4] + " "; 
    tempMsg += gameDie[5] + " "; 
    alert(tempMsg); 
} 

私も、私は何をする必要があるかもしれませんが、乱数を発生させるものは何でも、アラートの表示だと思います。私はちょうどそれをする方法を知らない。

これらは、私の乱数の世代を制御する関数です。

function randomNum(zMin, zMax) { 
    // This function generates, and returns, a random integer 
    // between zMin and zMax 
    return Math.floor(Math.random()*(zMax-zMin+1) + zMin); 
} 

function rollButtonClick() { 
    rollNum++; 
    // loop through each of the 5 game dice and set its "src" image 
    // to one of the 6 random hidden images 
    for(i=1; i<=5; i++) { 
     if (gameDieHold[i] === false) { 
     var tempNumber = randomNum(1,6); 
     gameDie[i].src = srcImage[tempNumber].src; 

     if (rollNum === 3) { 
      canRoll = false; 

      if (canRoll === false) { 
       console.log("working"); 
        gameDieHold[1] = true; 
        gameDieHold[2] = true; 
        gameDieHold[3] = true; 
        gameDieHold[4] = true; 
        gameDieHold[5] = true; 

        renderGameDieSpans(); 

        rollButton.removeEventListener("click", rollButtonClick, false); 
        rollButton.disabled = true; 


       } 
      } 

     } 
    }   
} 

助けてもらえるとうれしいです。長い一日だったし、私の脳は少し疲れていると思う。ありがとうございました。明確にする必要があるものがあれば教えてください。

答えて

0

alert(tempMsg)は、DOM要素document.querySelector("#gameDie1")を表示するように設定されています。それを変更してIDを表示するか、そのDOM要素のカスタム属性を代わりに追加します。

あなたは使用することができます。

gameDie[i].match(/\d/) 

または

gameDie[i].attributes.customAttr.value 

this

のように