【发布时间】:2018-12-16 06:46:58
【问题描述】:
所以我在做一个猜词游戏,
在我的 Game.js 文件中,我有一个名为 Game 的类,它有一个名为 checkForwin() 的方法,用于检查玩家是否选择了游戏中的所有字母,它基于我看到的 console.log 消息工作
checkForWin() {
//phraseMatch is the length of the empty boxes
const phraseMatch = $('.letter').length;
//match is the length of the displayed boxes with the class of show
const match = $('.letter.show').length;
//if the length of the empty boxes is equal to the length of the displayed boxes then return true
if (phraseMatch === match) {
console.log(phraseMatch + match + ' (the player has chosen all letters in the phrase')
return true
}
}
我被困在一个名为gameOver()的方法上,如果玩家输了,这个方法会显示红屏,如果玩家赢了,会显示绿屏,
我可以选择 ID 为 #game-over-message 的 h1,以便在玩家输掉比赛时通过添加类('loose a')来更改屏幕的消息和颜色,但是当我尝试使用相同的 ID 时第二次说
else if (this.checkForWin === true || this.missed < 5) {
$('#game-over-message').text('You won!');
$('#overlay').show().addClass('win a');
$('#btn__reset').text('Try Again!');
}
我似乎无法通过在我的 css 文件中添加类 ('win a') 来访问 id 以显示获胜颜色
gameOver() {
//if the player misses 5 times, display the game over message from index.html
if (this.missed === 5) {
$('#game-over-message').text('Game Over You Lost');
$('#overlay').show().addClass('lose a');
$('#btn__reset').text('Try Again!!');
} else if (this.checkForWin === true || this.missed < 5) {
$('#game-over-message').text('You won!');
$('#overlay').show().addClass('win a');
$('#btn__reset').text('Try Again!');
}
}
有人可以帮忙吗?这是我的仓库供参考https://github.com/SpaceXar20/FSJS-techdegree-project-4-b
【问题讨论】:
-
'win a'是两个独立的类 -
我有类似的东西,当我使用 $('#overlay').show().addClass('lose a') 时,它似乎工作正常,只是在我使用 $( '#overlay').show().addClass('win a');,你能详细说明你的答案吗
-
我不太明白你的问题,
I can't seem to access the id in order to display the winning color by adding the class ('win a')- 你看起来怎么样? -
啊,是的,请允许我澄清一下,在我的 html 文件中,我有这个 h1:
<h1 id="game-over-message"></h1>,这是我指的 id
标签: javascript