我再次问另一个问题,关于我正在为学校开发的Simon游戏的一些代码。 我的布尔值设置为false,游戏首先检查按键,然后将值设置为true。如果用户得到的游戏顺序不正确,则应将值重置为false。出于某种原因,我的代码没有在需要时来回更改布尔值,我不确定为什么。
/*************VARIABLES*************/
//store colors
var buttonColors = [
"green", //0
"red", //1
"yellow", //2
"blue" //3
]
//Game Pattern Storage
var gamePattern = [ /*Added From nextSequence*/ ];
var userClicks = [ /* Added from userClickHistory*/ ];
var level = 0;
var gameOn = false;
/******************BASIC FUNCTIONS*********************/
/*AWAIT KEYPRESS TO BEGIN GAME*/
$(document).keypress(function () {
if (!gameOn) {
nextSequence();
$(`#level-title`).text(`Level: ` + level);
var gameOn = true;
}
});
//log user clicks after nextSequence() has executed, check the userClicks vs gamePattern using checkAnswer(lastInArray)
$(`.btn`).click(function () {
var buttonClicked = $(this).attr(`id`);
userClicks.push(buttonClicked);
animate(buttonClicked);
playSound(buttonClicked);
checkAnswer(userClicks.length - 1);
});
function checkAnswer(usersLastClick) {
//if the gamePatterns last call is equal to the users last click
if (gamePattern[usersLastClick] === userClicks[usersLastClick]) {
console.log("success");
//if the userClicks and the gamePatterns lengths are equal, call nextSequence()
if (userClicks.length === gamePattern.length) {
setTimeout(function () {
nextSequence();
//update the titles level indicator
$(`#level-title`).text(`Level: ` + level);
}, 1000);
}
} else {
$(`#level-title`).text(`You have made it to level ` + level + '! Hit enter to try again')
//play sound for incorrect click
var wrongSound = new Audio('sounds/wrong.mp3');
wrongSound.play();
console.log("wrong")
console.log(`[` + userClicks + `]`);
console.log(`[` + gamePattern + `]`);
reset();
}
}
/************* NEXT SEQUENCE TO PROGRESS GAME *********/
function nextSequence() {
userClicks = [];
level++;
console.log(level);
randomNumber = Math.floor(Math.random() * 4)
randomChosenColor = buttonColors[randomNumber];
gamePattern.push(randomChosenColor);
animate(randomChosenColor);
playSound(randomChosenColor);
}
function reset() {
var level = 0;
var gamePattern = [];
var gameOn = false;
}
/******************** SOUNDS AND ANIMATIONS*************************************/
//buttons animations
function animate(clickedButton) {
$(`#` + clickedButton).fadeOut(100).fadeIn(100);
};
//Play a sound in correlation to randomChosenColor
function playSound(color) {
var sound = new Audio('sounds/' + color + '.mp3');
sound.play();
};
当我的代码运行时,布尔值在里面什么地方都没有改变,而且我还注意到当游戏也想重置时,我的数组并没有被清空。当我console.log(gameOn)时,无论游戏是否运行,它始终返回false。
答案 0 :(得分:1)
在您的$(document).keypress
函数中,尝试在“ var
”之前取出“ gameOn = true
”。我想现在,您好像真的要更改全局gameOn
var时就在声明一个新变量。
答案 1 :(得分:0)
if (!gameOn) { nextSequence(); ..... var gameOn = true; }
不要再次声明gameOn变量。您可以直接使用 gameOn = ... 变量,因为它是在函数的外部范围内声明的,并且在内部范围内
答案 2 :(得分:0)
正如其他人所说,不要两次声明gameOn,它应该可以解决。
但是我一直在读越来越多的关于JavaScript中全局变量的“面纱”,因为全局命名空间意味着任何人都可以编辑这些变量,甚至其他脚本。
许多人似乎倾向于使用的解决方案是将变量包装在对象中,如下所示:
var globals = {
buttonColors: [ "green", "red", "yellow", "blue"],
gamePattern: [ /*Added From nextSequence*/ ],
userClicks: [ /* Added from userClickHistory*/ ],
level: 0,
gameOn: false,
}
然后,仅使用“ globals.buttonColors”来引用变量。
如果您打算将范围保持较小,则不是100%必要的,但是不管怎样,这都是一个好习惯。