需要帮助纠正文字游戏中的功能 [英] Need help correcting a function in text game

查看:87
本文介绍了需要帮助纠正文字游戏中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图教自己如何编写简单的文字游戏.我找到了YouTube教程,并且正在通过Der Kerker进行工作.我的问题是他的最终代码与他在视频中所做的不匹配,因此我陷入了困境.

I'm trying to teach myself how to code a simple text game. I found a YouTube tutorial and am working my way through Der Kerker. A problem I have is that his final code doesn't match what he did in the videos, so I'm stuck.

这是我的问题:

加载游戏时,命令接剑"和帮助"均按设计工作.但是,如果您输入了胡言乱语或无法识别的命令,则游戏应该会说:我听不懂..."

When you load the game, the commands "take sword" and "help" both work as designed. However, if you put in jibberish or an unrecognized command, the game is supposed to say, "I don't understand ... "

现在,它仅清除输入框,但实际上不运行命令.

Right now, it only clears the input box but doesn't actually run the command.

这是我玩的小提琴:

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

  1. 如果我删除了(check == false)部分-那么我听不懂"部分将起作用.但是,如果您键入"help"-它会运行帮助部分并说我不理解帮助".

目标是,如果玩家键入无法识别的命令,它只会说我听不懂".

The goal is for it to only say "I don't understand" if the player types in an unrecognized command.

这是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("");
    });
});

希望如此.

推荐答案

我认为这是您想要的:

已替换代码:

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

代码段:

//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("");
	});
	
});

<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>

这篇关于需要帮助纠正文字游戏中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆