我开始做一个文字游戏,其基本思想是: 游戏开始时,用户必须输入自己的,选项到输入字段。他输入的文字,与功能内部的开关进行交互,如果用户输入“正确的命令”,它调用另一个函数,可包括另一开关等。 我在这段代码中有一个错误: 第一个函数内部的开关运行良好,当它的第一个case(case“yes”)调用另一个函数时,一切都开始正常工作。但是当用户输入应该与第二个功能的开关交互的文本时,他实际上仍然与第一个功能的开关(以及第二个功能)交互。 会有人愿意帮我解决它,甚至点我在正确的方向?JS - 如何在另一个启动时停止执行第一个开关?
var startGame = function(){
showText("Welcome to the virtual pseudo-reality")
showText("Are you ready to start your journey?")
$('#btn').click(function(e) {
e.preventDefault();
switch ($('input[name=myInput]').val().toLowerCase()) {
case "yes": showText("- "+$('input[name=myInput]').val());
showText("Welcome aboard!");
selectChar();
break;
case "no": showText("- "+$('input[name=myInput]').val());
showText("Your choise is no.");
break;
default:
showText("- "+$('input[name=myInput]').val());
showText("Sorry, wrong input.") ;
break;
};
$('input[name=myInput]').val('');
var element = document.getElementById("storyBoard");
element.scrollTop = element.scrollHeight;
});
};
//A first in-game function, needs to select the character that user want to be:
var selectChar = function(){
showText("<br>"+"Choose who you are, you have to choose one of two heroes: Phantomorph and Disogr.");
$('#btn').click(function(e) {
e.preventDefault();
switch ($('input[name=myInput]').val().toLowerCase()) {
case "phantomorph": showText("- "+$('input[name=myInput]').val());
showText("You choosed Phantomorph.");
user = phantomorph; friend = disogr;
break;
case "disogr": showText("- "+$('input[name=myInput]').val());
showText("You choosed Disogr.");
user = disogr; friend = phantomorph;
break;
default: alert("You need to make a right choose.");
selectChar();
break;
};
$('input[name=myInput]').val('');
var element = document.getElementById("storyBoard");
element.scrollTop = element.scrollHeight;
});
};
startGame();
谢谢,我试图关注你的建议,但我如何绑定主要功能以外的新事件?我真的不明白它是如何工作的。 – MacroG0D
我已经更新了我的答案,以包含关于在哪里绑定事件以及如何通过单击处理程序处理用户交互的建议。 – AndFisher