Simon游戏JavaScript问题:序列正确时无法进入下一级
问题修复:Simon游戏序列正确后无法进入下一级
你的代码核心问题是拼写错误,导致序列验证通过后无法触发下一级逻辑:
在checkAnswer函数中,你写了gamePattern.lenngth——这里多了一个字母n,正确写法是gamePattern.length。因为这个错误,判断用户序列长度是否等于游戏序列长度的条件永远不成立(undefined和数字无法相等),所以不会执行setTimeout调用nextSequence进入下一级。
修正后的checkAnswer函数代码:
function checkAnswer(currentLevel) { //Checking if game pattern and user pattern is right or wrong if (gamePattern[currentLevel] === userClickedPattern[currentLevel]) { if (userClickedPattern.length === gamePattern.length){ //Calling nextSequence after 1 second delay setTimeout(function(){ nextSequence(); },1000); } }else{ //to play wrong sound from sound folder if answer is wrong playSound("wrong"); //Adding css property $("body").addClass("game-over"); //for gameover title $("#level-title").text("Game Over, Press Any Key to Restart"); //Remove CSS property after .2 second setTimeout(function(){ $("body").removeClass("game-over"); },200); startOver(); } }
只需要修正这一处拼写错误,当用户点击的序列完全匹配游戏生成的序列时,程序就会在1秒延迟后调用nextSequence,清空用户输入序列、提升等级并生成新的序列,进入下一级游戏。
内容的提问来源于stack exchange,提问作者bash
相关产品推荐
相关产品推荐

