2014-03-25 41 views
0

我最近完成了Javascript,CSS,HTML的基礎知識,並且已經啓動了jQuery。我決定我足夠了解一個簡單的hang子手遊戲,所以我決定這麼做。我已經完成了大部分工作,但是如果他們猜到正確的字母,我似乎無法讓遊戲取代底部的破折號。此外,如果他們猜出正確的字母,遊戲會揭示身體部位,即使它不應該。我必須測試這些字母的代碼如下。整個代碼是here。由於此代碼在jsfiddle中無法發揮最佳效果,因此您可以下載源代碼here(或者只是從jsfiddle中複製它)。Hang子手遊戲沒有識別正確的字母猜到

的部分我似乎遇到的麻煩:

function letterCheck(){ 
    for(var z=0; z < trueWord.length; z++){ 
     if(trueWord[z] == letter){ 
      trueWord[z] = letter; 
      numberOfRightLetters++; 
      alert("Correct!"); //Test to see if player guessed correct letter 
      document.getElementById('bottom').innerHTML="<p> Current Word: " + displayedWord + "</p>"; //SUPPOSED to place the letter at the bottom if guessed correctly **not working** 
     } else { 
      wrongLetter = true; //Tells program that the player guessed the wrong letter 
      alert("Incorrect!"); //Test to see if player guessed correct letter 
     } 
    } 

    if(wrongLetter == true){ 
     numberOfWrongLetters++; //Increases the number of times the player guessed a wrong letter so the program displays the correct body part 

     //Displays person and their body parts 
     switch(numberOfWrongLetters){ 
      case(1): 
       $('#head').fadeIn("slow"); 
      break; 
      case(2): 
       $('#body').fadeIn("slow"); 
      break; 
      case(3): 
       $('#LArm').fadeIn("slow"); 
      break; 
      case(4): 
       $('#RArm').fadeIn("slow"); 
      break; 
      case(5): 
       $('#Lleg').fadeIn("slow"); 
      break; 
      case(6): 
       $('#Rleg').fadeIn("slow"); 
      break; 
     } 
    } 
+0

我沒有看到你設置' wrongLetter'到'false'。從邏輯上講,你應該在'letterCheck'函數中將變量初始化爲'false',或者在提醒'Correct!'的'if語句中設置它。當你第一次檢查'trueWord [z] == letter',然後如果它設置了'trueWord [z] = letter',你又該怎麼做?如果條件已經存在,爲什麼要做出該聲明? – Jasper

+0

@Jasper我在函數中聲明'wrongLetter'爲false,該函數檢測玩家點擊的字母並使字母消失。我明白了'trueWord [z] == letter'然後是'trueWord [z] = letter'的意思,它應該是'trueWord [z] == letter' then'displayedWord [z] == letter'。 – Jeff

+0

看看是不是所需的行爲http://jsfiddle.net/Gcd87/1/? –

回答

0

基本上我做了一些修改功能letterCheck

function letterCheck(letter){ 

    for(var z=0; z < trueWord.length; z++){ 
     if(trueWord[z] == letter){ 
      numberOfRightLetters++; 
      wrongLetter = false; 
      //Iterate the true word and when we find letters that match set the letter 
      //on the displayed word to the same position it was found on the trueword 
      $.each(trueWord,function(l){ 
        //l is the index of the letter in the array 
       if(trueWord[l] == letter){ 
        //replace al the occurrences of the letters on the displayed word 
        displayedWord[l] = letter 
       } 
      }) 
      //Removed the alert in each iteration to the if(wrongLetter == true) 
      document.getElementById('bottom').innerHTML="<p> Current Word: " + displayedWord + "</p>"; //SUPPOSED to place the letter at the bottom if guessed correctly **not working** 
      //Added a break to stop the loop on the first ocurrence of the letter 
      //this is made to have control on the wrongLetter flag 
      break; 
     } else { 
      wrongLetter = true; //Tells program that the player guessed the wrong letter 

     } 
    } 

    if(wrongLetter == true){ 
     alert("Incorrect") 
     numberOfWrongLetters++; //Increases the number of times the player guessed a wrong letter so the program displays the correct body part 

     //Displays person and their body parts 
     switch(numberOfWrongLetters){ 
      case(1): 
       $('#head').fadeIn("slow"); 
      break; 
      case(2): 
       $('#body').fadeIn("slow"); 
      break; 
      case(3): 
       $('#LArm').fadeIn("slow"); 
      break; 
      case(4): 
       $('#RArm').fadeIn("slow"); 
      break; 
      case(5): 
       $('#Lleg').fadeIn("slow"); 
      break; 
      case(6): 
       $('#Rleg').fadeIn("slow"); 
      break; 
     } 
    }else{ 
     alert("Correct!") 
     //Added to display one alert correct and not one for each letter 
    } 

Fiddle