我在做一个危险的游戏。我可以让用户选择一个方块,问题出现在输入框中,他们点击提交,答案与正确答案进行比较。如果正确,请这样说,并将积分加到总数中;如果错误,请这样说,并从总数中扣除积分。但随后游戏就卡住了。我想不出如何让游戏为下一道题的选择做好准备。
下面是第一个问题的js代码。如果你想要更多的代码,请告诉我(我不想让这个问题变得混乱)。可能有一个简单的解决方案,所以如果你想知道“为什么不直接使用______呢?”答案是因为我是一个菜鸟,我不知道怎么做。非常感谢你提前这么做。
codepen:http://codepen.io/Nplagma/pen/XNerRw
$(document).ready(function() {
$(".question").hide();
var score = 0;
$(".well").hover(function() {
$(this).addClass('blue');
}, function() {
$(this).removeClass('blue');
});
$("#A1").click(function() {
var questionValue = $(this).data("questionvalue");
$(".question").show();
$('<h4>The \"MVP\" quarterback whose team is 14-6 when he doesn’t play.</h4>').appendTo('.question');
$('#submit').click(function() {
$("#answer").on('input')
var answer = $('#answer').val(); //what does this mean in words?
if
(answer == "Tom Brady" || answer == "Brady" || answer == "brady" || answer == "tom brady") {
$('.question').replaceWith('<h3>omg you\'re so smart</h3>') //using h3 because it'll be unique, but there must be a better way
score += questionValue;
$(".score").text("$" + score);
}
else
{
$('.question').replaceWith('<h3>could NOT have been more wrong</h3>');
score -= questionValue;
$(".score").text("$" + score);
}
});
});发布于 2016-12-01 03:08:52
仅供参考。你选择的方法可能不是完成这项任务的最佳方法。你的用户可以很容易地检查代码来找到你问题的答案。一种“更好”的方法是将所有数据(即问题和答案)存储在数据库中,并使用ajax进行访问。然后,您还需要学习后端语言和SQL。
您也在使用不同的状态。当这变得更复杂时,你会发现控制它们并跟踪所有事情是一件痛苦的事情。这才是前端框架真正闪亮的地方。我建议你去看看。现在有很多这样的工具: React,Angular,Meteor,Vue,Backbone等等。看看它们,选择一个对你来说最有意义的。(
但是,如果你只是想让这个应用程序更好地学习javascript。继续你的方法:)
https://stackoverflow.com/questions/40842780
复制相似问题