简体   繁体   English

让用户在三次尝试后继续进行

[英]Letting user carry on after three attempts

In my spelling game there is a grid that is populated with words. 在我的拼写游戏中,有一个填充了单词的网格。 The words are hidden and the aim of the game is to spell the word that is highlighted with the aid of a sound and a picture. 这些单词是隐藏的,游戏的目的是拼写借助声音和图片突出显示的单词。

To highlight a word you press the "next" button. 要突出显示单词,请按“下一步”按钮。 At the moment if you spell the word correctly it says "well done" and you can advance to the next word, but if you spell it incorrectly you have to keep attempting the word until it is complete. 目前,如果你拼写正确的单词,它说“做得好”,你可以前进到下一个单词,但如果你拼写错误,你必须继续尝试这个单词,直到它完成。

As the game is designed for children I do not think this is the best approach, so I would like to make it so you can advance after 3 incorrect attempts. 由于游戏是为儿童设计的,我不认为这是最好的方法,所以我想这样做,这样你可以在3次不正确的尝试后前进。

I have played around with the script so much trying to put counters on incorrect attempts and then making the button active but cannot seem to get it to work. 我已经玩过脚本这么多尝试将计数器置于不正确的尝试,然后使按钮处于活动状态,但似乎无法让它工作。 Can someone please help me? 有人可以帮帮我吗?

Here is the script for the button 这是按钮的脚本

var noExist = $('td[data-word=' + listOfWords[rndWord].name + ']').hasClass('wordglow2');
if (noExist) {
    $('.minibutton').click();

} else {
    $('.minibutton').click('disable');
    $("#mysoundclip").attr('src', listOfWords[rndWord].audio);
    audio.play();
    $("#mypic").attr('src', listOfWords[rndWord].pic);
    pic.show();
}
});

"wordglow2" is the style applied if the word is spelt correctly. “wordglow2”是单词拼写正确时应用的样式。 Here is a fiddle to help understand... http://jsfiddle.net/smilburn/ZAfVZ/4/ 这是帮助理解的小提琴...... http://jsfiddle.net/smilburn/ZAfVZ/4/

Add a global variable to your script 向脚本添加全局变量

var numberOfTries = 0;

Next, in your $('.drag').on('click') listener callback, you have an if statement that determines the word being correct or incorrect: 接下来,在$('。drag')。on('click')侦听器回调中,您有一个if语句,用于确定单词是正确还是不正确:

if (!$('.drop-box.spellword:not(.occupied)').length)

Inside the if statement, reset numberOfTries to 0. Inside the else statement, you will put: 在if语句中,将numberOfTries重置为0.在else语句中,您将放置:

numberOfTries++;
if (numberOfTries >= 3)
{
    $('.minibutton').prop('disabled', false);
}

This will count the number of tries, and after the third try, will enable the "next" button. 这将计算尝试次数,并在第三次尝试后,将启用“下一步”按钮。 Once the user gets a word correct, it will reset the variable to 0. 一旦用户获得正确的单词,它将把变量重置为0。

What you can do is use jQuery's .data() function to attach a counter to each TR. 你可以做的是使用jQuery的.data()函数将计数器附加到每个TR。 On an unsuccessful attempt, increment the counter and make the decision whether to disable or enable the button based off of that. 在尝试失败时,递增计数器并根据该值确定是否禁用或启用按钮。 I've modified the jsfiddle you sent and added this behavior. 我修改了你发送的jsfiddle并添加了这个行为。 Take a look at the line (You can do a Ctrl+F to find it) that has the comment // Edits here 看看有这条评论的行(你可以用Ctrl + F找到它)//在这里编辑

http://jsfiddle.net/dflor003/ZAfVZ/7/ http://jsfiddle.net/dflor003/ZAfVZ/7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM