尽管使用了off,click函数仍会触发,这可能也会导致数组问题
click function firing despite using off, probably causing the array issue too
我正在开发一款西蒙游戏,我觉得我已经接近尾声了(耶!),不过我似乎遇到了一个问题。在玩家匹配西蒙的第一个移动后,它将移动到下一轮,并用第二个移动填充阵列。然后,您可以匹配第二个移动,它也可以正确地迭代。但我认为它是第二次发射,然后通常是错误的,然后使玩家阵列将一步作为最后一步,而不是重置。
如何确保按钮只触发一次,以及如何正确重置玩家阵列?
function playerInput(simon, player, j){
while (j < simon.length)
{
console.log(j);
if(simon[j] === player[j])
{
console.log("player pressed the correct button");
j++;
// potentially not working off function
$(".simon-button").off().on('click', function() {
// stops multiple executions
});
buttons(simon, j, player);
}
else
{
if(strict === true)
{
reset();
return;
}
else
{
console.log("player pressed the wrong button");
// seems to be screwing up here when it's wrong
// but not when used to reset player when it's right
player = [];
repeatMoves(player);
return;
}
}
}
if(j >= simon.length && simon.length < 20)
{
j = 0;
player = [];
newMove(player);
}
else
{
alert("You win!!!");
successSound.play();
reset();
}
}
function buttons(simon, j, player){
green.css('cursor', 'pointer');
red.css('cursor', 'pointer');
blue.css('cursor', 'pointer');
yellow.css('cursor', 'pointer');
$(".simon-button").click(function(){
if($(this).hasClass("green-button"))
{
player.push(greenPushed());
playerInput(simon, player, j);
}
else if($(this).hasClass("red-button"))
{
player.push(redPushed());
playerInput(simon, player, j);
}
else if($(this).hasClass("blue-button"))
{
player.push(bluePushed());
playerInput(simon, player, j);
}
else if($(this).hasClass("yellow-button"))
{
player.push(yellowPushed());
playerInput(simon, player, j);
}
console.log(player);
console.log(simon);
});
}
https://jsfiddle.net/uoyg2jx9/1/
我忘了提一下,为了让小提琴工作,点击空按钮,然后点击启动按钮。
我还没有充分消化你的代码,无法理解它应该做什么,但它确实有一点无休止的循环:
while (j < simon.length) {
if(simon[j] === player[j]) {
// ...
j++; // ok
// ...
} else {
// nothing in this clause modifies j. To infinity and beyond...
}
}
这里有一个i=0;
,之前没有提到i
,所以也许这就是问题所在(尽管将j设置为零仍然会让你陷入无尽的循环。)我怀疑你想在else
中打破while循环,也许是通过设置j == simon.length
,或者在repeatMoves()
之后设置return
ing。
相关文章:
- 将子文档推送到父数组时遇到问题
- 从数组在d3中创建条形图时出现问题
- JavaScript/jQuery JSON数组问题-值为未定义
- JavaScript变量引用数组时出现问题
- Javascript中的多维数组排序索引问题
- JavaScript对象中存在数组的问题
- array_push平面数组问题-需要能够向数组中添加多个变量
- 多维数组 JAVASCRIPT 出了点问题
- javascript中的数组和if语句存在问题
- JavaScript的循环问题,将值插入数组将不起作用
- Javascript数组比较问题
- 设置和添加数组值javascript时出现问题
- 数组有什么问题
- 在Javascript/Google Maps Api中将结果放入数组时出现问题
- 尽管使用了off,click函数仍会触发,这可能也会导致数组问题
- azure移动服务中的数组长度问题
- 将数组从PHP传递到Javascript(Jquery)的问题,如常见答案所示
- 填充对象的Javascript数组时出现问题
- JavaScript——迭代一系列问题(数组/对象)
- 表单显示问题+数组下的结果