我最近完成了Javascript,CSS,HTML的基础知识,并且已经启动了jQuery。我决定让我知道一个简单的刽子手游戏,所以我决定这样做。我已经完成了大部分工作,但是如果他们猜出正确的字母,我似乎无法让游戏替换底部的破折号。此外,如果他们猜测正确的字母游戏显示身体部位,即使它不应该。我必须测试的字母代码如下。整个代码是here。由于此代码在jsfiddle中不起作用,您可以下载源here(或者只是从jsfiddle复制它)。
我似乎遇到麻烦的部分:
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 :(得分: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
}