【问题标题】:Slow down, console. (javascript/jquery)慢点,安慰。 (javascript/jquery)
【发布时间】:2015-11-12 05:43:21
【问题描述】:

我正在开发一个控制台游戏,我不喜欢 console.logs 的速度以及提示之间的时间太短等等。有没有 javascript/jquery 方法来减慢游戏速度?为此,我可以简单地delay() 每一行(这听起来很乏味),或者如果我要使用setTimeout(),理论上我是否必须将我的游戏分成许多不同的功能并设置超时或间隔?你有什么建议?

sn-p 例如:

alert('~~ WELCOME TO [x] ~~');
console.log('Before we get started, let\'s find out a little about you.');
var usr = {
    name : prompt('What\'s your name?'),
    age : prompt('How old are you?'),
    clr : prompt('What\'s your favorite color?'),
    pref : prompt('Which [x] is your favorite?'),
}


console.log('The air smells pungent today, and the weather is perfect.');
console.log(''); console.log('');
console.log('Did you hear that? I think something may be following us down the path to the [x]...');
console.log('');


var alpha = prompt('');

会有if/elsesswitches,各种功能和选择。但我想要基于文本的主机游戏的感觉。

我计划在某个时候添加许多不同的路线、功能和运动。但这都是无关紧要的。如果有人知道一两种方法可以降低遵循本指南的游戏速度,请发表任何建议。

【问题讨论】:

    标签: javascript jquery console


    【解决方案1】:

    大多数用户认为提示既烦人又丑陋。在执行提示期间,用户将无法与其他任何东西进行交互,包括其他选项卡或控制台。此外,作为开发人员使用它非常不方便,因为它们不可配置,并且难以开发、支持,尤其是调试。

    最好实现一个与用户交互的 HTML 页面。因此,您将能够自定义它并看起来不错。

    例如,您可以创建一个看起来像聊天的页面 - 文本窗口并在底部输入。像这样:

    function tell(text) 
    {
      $("<p/>").text(text).appendTo($('#chat'));
    }
    
    function ask(text, callback)
    {
      $("<p/>").text(text).addClass('question').appendTo($('#chat'));
      
      $('#send')
        .prop('disabled', false)
        .one('click', function() {
          var text = $("#message").val();
          callback(text);
          $("#message").val("");
          $(this).prop('disabled', true);
        });
    }
    
    tell("Hi");
    
    ask("What is your name?", function(x) {
      tell("So strange...my name is " + x + ", as well...");  
    });
    #chat {
      padding: 20px;
      position: absolute;
      top: 0px;
      bottom: 20px;
      left: 0px;
      right: 0px;
      background-color: #DDDDDD;
    }
    
    #message {
      position: absolute;
      bottom: 0px;
      left: 0px;
      height: 14px;
      width: 80%;
    }
    
    #send {
      position: absolute;
      bottom: 0px;
      left: 80%;
      width: 20%;
      height: 20px;
    }
    
    .question {
      font-weight: bold;
      color: red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="chat"></div>
    <input type="text" id="message"/>
    <input type="submit" id="send" disabled/>

    这只是一个例子。您可以添加任何东西,例如延迟或 CSS 样式。

    【讨论】:

    • 你能解释一下propone方法吗?
    • @PhilC。 jQuery.prop() 改变 DOM 元素的属性。在这个例子中,它只设置了disabledjQuery.one() 应用一个仅运行一次的事件处理程序。
    【解决方案2】:

    创建您自己的 consolealertprompt 方法来包装原生对象。

    例如:

    function logConsole(text, delay) {
      window.setTimeout(function() { 
         console.log(text);
      }, delay || 0);
    };
    

    如果没有传入延迟参数,您可以将上面的0 值更改为默认延迟。

    logConsole('The air smells pungent today, and the weather is perfect.', 1000);
    

    只是一个想法

    【讨论】:

    • 不过是个好主意。我喜欢。我会搞砸一段时间。谢谢!
    猜你喜欢
    • 2017-03-30
    • 2021-10-03
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多