需要帮助纠正文本游戏

时间:2015-10-27 22:40:54

标签: javascript jquery

我正在努力教自己如何编写简单的文字游戏。我找到了一个YouTube教程,正在通过Der Kerker工作。我遇到的一个问题是他的最终代码与他在视频中所做的不符,所以我被卡住了。

这是我的问题:

当您加载游戏时,命令“take sword”和“help”都按设计工作。但是,如果你输入乱码或无法识别的命令,游戏应该说“我不明白......”

现在,它只清除输入框但实际上并没有运行命令。

这是我的游戏小提琴:

https://jsfiddle.net/megler/hv7hqp1e/

  1. 如果我删除(check == false)部分 - 那么“我不理解”部分将起作用。但是,如果您输入“帮助” - 它将运行帮助细分并说“我不理解帮助”。
  2. 目标是,如果玩家输入无法识别的命令,它只会说“我不明白”。

    这是JS:

    //Check To See If True Function
    var check = false;
    
    // Been To Room Variables
    var beenToCorridor = true;
    //
    
    //Inventory
    var sword = false;
    //
    
    //Current Room 
    var currentRoom = "nCorridor";
    
    $(document).ready(function() {
    
        $("form").submit(function() {
            var input = $("#commandLine").val();
    
            function check() {
                check = true;
            }
    
            if (input == "help") {
              $("#messageHelp")
                .clone()
                .insertBefore("#placeholder")
                .fadeIn(1000);
    
              check();
            }
            if (input == "take sword" && currentRoom == "nCorridor") {
              $("<p>You picked up a sword.</p>")
                .hide()
                .insertBefore("#placeholder")
                .fadeIn(1000);
    
              check();
            }       
            else if (input == "take sword" && currentRoom != "nCorridor") {
              $("<p>The sword is not here.</p>")
                .hide()
                .insertBefore("#placeholder")
                .fadeIn(1000);
    
              check();
            }
            else if (check == false) {
              $("<p>I don't understand " + input +  ".</p>")
                .hide()
                .insertBefore("#placeholder")
                .fadeIn(1000);
            }
    
            $("#commandLine").val("");
        });
    });
    

    希望这是有道理的。

5 个答案:

答案 0 :(得分:1)

我认为这就是你想要的:

代码替换:

else if (input != "take sword" && input != "help") {
            $("<p>I don't understand " + input +  ".</p>").hide().insertBefore("#placeholder").fadeIn(1000);
        }

<强>段:

&#13;
&#13;
//Check To See If True Function
var check = false;

// Been To Room Variables
var beenToCorridor = true;
//

//Inventory
var sword = false;
//

//Current Room 
var currentRoom = "nCorridor";

$(document).ready(function() {
	
	$("form").submit(function() {
		var input = $("#commandLine").val();
		
		function check() {
			check = true;
		}
		
		if (input == "help") {
			$("#messageHelp").clone().insertBefore("#placeholder").fadeIn(1000);
			check();
		}
		
		if (input == "take sword" && currentRoom == "nCorridor") {
			$("<p>You picked up a sword.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
			check();
		}
		
		else if (input == "take sword" && currentRoom != "nCorridor") {
			$("<p>The sword is not here.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
			check();
		}
		else if (input != "take sword" && input != "help") {
			$("<p>I don't understand " + input +  ".</p>").hide().insertBefore("#placeholder").fadeIn(1000);
		}
        else
        {
            return false;
        }
		$("#commandLine").val("");
	});
	
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  
  <div id="console">
  
  <p id="message_startGame">Welcome to my game!</p>
  
  <p id="area_northCorridor">You are in the North Corridor.  There is a sword on the ground.</p>
  
  <p id="messageHelp" style = "display: none;">Here is a list of commands</p>
  
  <!-- PLACEHOLDER:  THIS IS WHERE EVERYTHING WILL BE INSERTED BEFORE
  --->
  
  <div id="placeholder"></div>
  
  <form onsubmit="return false;">
  <input type = "text" size ="50" autofocus="autofocus" id="commandLine">
  </form> 
	
 </div>
  </body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

检查变量使用不同的名称。如果,您还应该将最后的分开。

//Check To See If True Function
var _check = false;

// Been To Room Variables
var beenToCorridor = true;
//

//Inventory
var sword = false;
//

//Current Room 
var currentRoom = "nCorridor";

$(document).ready(function() {

    $("form").submit(function() {
        var input = $("#commandLine").val();

        function check() {
            _check = true;
        }

        if (input == "help") {
          $("#messageHelp")
            .clone()
            .insertBefore("#placeholder")
            .fadeIn(1000);

          check();
        }
        if (input == "take sword" && currentRoom == "nCorridor") {
          $("<p>You picked up a sword.</p>")
            .hide()
            .insertBefore("#placeholder")
            .fadeIn(1000);

          check();
        }       
        else if (input == "take sword" && currentRoom != "nCorridor") {
          $("<p>The sword is not here.</p>")
            .hide()
            .insertBefore("#placeholder")
            .fadeIn(1000);

          check();
        }

        if (_check == false) {
          $("<p>I don't understand " + input +  ".</p>")
            .hide()
            .insertBefore("#placeholder")
            .fadeIn(1000);
        }

        $("#commandLine").val("");
    });
});

答案 2 :(得分:0)

你应该

  • 首先在某处初始化var check = false;,否则if条件永远不会通过
  • 在您的if ... else if ...支票列表中添加其他内容。

以下是更正后的代码:

$(document).ready(function () {

    $("form").submit(function () {
        var input = $("#commandLine").val();
        // INITIALIZE CHECK VARIABLE HERE
        var check = false;

        function check() {
            check = true;
        }

        if (input == "help") {
            $("#messageHelp").clone().insertBefore("#placeholder").fadeIn(1000);
            check();
        } 
        // NEEDS AN ADDITIONAL ELSE HERE TO PREVENT DOUBLE MESSAGE AFTER HELP
        else if (input == "take sword" && currentRoom == "nCorridor") {
            $("<p>You picked up a sword.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
            check();
        } else if (input == "take sword" && currentRoom != "nCorridor") {
            $("<p>The sword is not here.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
            check();
        } else if (check == false) {
            $("<p>I don't understand " + input + ".</p>").hide().insertBefore("#placeholder").fadeIn(1000);
        }
        $("#commandLine").val("");
    });

});

答案 3 :(得分:0)

每次提交表单时,都会将check变量重置为false,因此每次调用表单时都要从一个干净的slate中取出t = tart。同时将函数'check'重命名为'setChecked',以免引起全局变量和本地函数名之间的混淆。

$("form").submit(function() {       
    check = false;

    function setChecked() {
        check = true;
    }

    if (input == "help") {          
        setChecked();           
    }

   //etc...
}

答案 4 :(得分:0)

这是因为你重新定义了检查&#39;作为一种功能,所以它不再等于假。

解决方案是使用另一个名称作为布尔值,例如&#39; ischeck&#39;,如下所示:

//Check To See If True Function
var ischeck = false;

// Been To Room Variables
var beenToCorridor = true;
//

//Inventory
var sword = false;
//

//Current Room 
var currentRoom = "nCorridor";

$(document).ready(function() {

    $("form").submit(function() {
        var input = $("#commandLine").val();

        function check() {
            ischeck = true;
        }

        if (input == "help") {
            $("#messageHelp").clone().insertBefore    ("#placeholder").fadeIn(1000);
            check();
        }

        if (input == "take sword" && currentRoom == "nCorridor") {
            $("<p>You picked up a sword.</p>").hide().insertBefore    ("#placeholder").fadeIn(1000);
            check();
        }

    else if (input == "take sword" && currentRoom != "nCorridor") {
        $("<p>The sword is not here.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
        check();
    }
    else if (ischeck == false) {
        $("<p>I don't understand " + input +  ".</p>").hide().insertBefore("#placeholder").fadeIn(1000);
    }
    $("#commandLine").val("");
});

});