Ich erstelle ein Abenteuerspiel mit html/css, js und jquery.Wie verbinde ich zwischen Benutzereingabe und Funktion?
Ich muss irgendwie Benutzereingabe (von HTML-Eingabefeld) zu js Schalter oder wenn sonst Anweisung, wenn Benutzer klickt auf Eingabe (fast wie eine prompt() -Funktion). Zum Beispiel, wenn das Spiel fragt ein Ja \ keine Frage, ich möchte ihn die Antwort in das Eingabefeld schreiben lassen, und diese Antwort nach dem Drücken der Eingabetaste senden, so dass das Spiel abhängig von den Entscheidungen, die er macht. Hier
ist der Code, den ich habe:
// This is a button click function. When user clicks the Enter button with the left mouse, all the text from the input appends to the "story board".
$("#btn").click(function(){
var btnClick = $("input[name=myInput]").val();
$("#storyBoard").append(btnClick+"<br>");
$("input[name=myInput]").val("");
var element = document.getElementById("storyBoard");
element.scrollTop = element.scrollHeight;
});
// It is an additional function for button click function, that allows user to press enter button on a keyboard to call the click button function.
$("#userInput").keyup(function(event){
if(event.keyCode == 13){
$("#btn").click();
}
});
/* Here i stucked. I writed a sketch switch(), but how do i connect between this and the user input? */
var mission1 = function(){
showText("Welcome to the virtual pseudo-reality.<br> Are you ready to start your journey?");
var readyToStart = function(){
switch(){
case "yes": console.log("yes");
break;
case "no": console.log("no");
break;
default: console.log("yes or no?");
readyToStart();
}
}
body {
border: .1em solid #232C01;
margin-top: 5em;
margin-left: 10em;
margin-right: 10em;
background-color: #070900;
background-image: url(images/Star-Wars-7-Poster-Banner.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: 100%;
}
#storyBoard{
border: .1em solid #f0ff00 ;
background-color: #000000 ;
margin-top: 2em;
margin-right: 5em;
margin-left: 5em;
height: 20em;
padding-left: 20px;
padding-right: 50px;
font-size: 50px;
color: #f3fd4e;
opacity: .95;
overflow:auto;
}
#userInput{
border: .05em solid #adae32;
margin-bottom: 2em;
margin-left: 5em;
font-size: 50px;
width: 71%;
color: #0033bd;
}
#btn{
font-size: 50px;
background-color: #0E1200;
color: #feff6c;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title>MacroG0D - My first game</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src ="Js/basicFight1vs1Function.js"> </script>
</head>
<body>
<div id = "storyBoard">
</div>
<input type="text" id ="userInput" placeholder="Type the command here" name="myInput"/>
<button id = "btn"> Enter </button>
</body>
</html>