简体   繁体   English

我的代码有什么问题? (JavaScript)

[英]What's wrong with my code? (JavaScript)

I'm coding in JS, I'm a beginner, and I'm trying to make a somewhat game?我正在用 JS 编码,我是一个初学者,我正在尝试制作一个游戏? I guess you could say.我想你可以说。 Anyway, here's the code无论如何,这是代码

<script>
    var FromStart = 0
    document.write("|");
    for (i = 0; i < FromStart; i++) {
    document.write("_");
    }
    document.write("O");
    for (i = 0; i < 30-FromStart; i++) {
    document.write("_");
    }
    document.write("|");

    function Left() {
      var FromStart += 1;
    document.write("|");
    for (i = 0; i < FromStart; i++) {
    document.write("_");
    }
    document.write("|");
    for (i = 0; i < 30-FromStart; i++) {
    document.write("_");
    }
    document.write("|");
    }


    function Right() {
    var FromStart += -1;
    document.write("|");
    for (i = 0; i < FromStart; i++) {
    document.write("_");
    }
    document.write("|");
    for (i = 0; i < 30-FromStart; i++) {
    document.write("_");
    }
    document.write("|");
    }

    </script><br>
    <button onclick="Left()">←</button>
    <button onclick="Right()">→</button>

This code isn't working, I don't know why.此代码不起作用,我不知道为什么。 Here's what I want it to be like:这是我想要的样子:

|_O_____________________| |_O_____________________| and then you press the right button 2 times, lets say... |___O___________________|然后你按两次右键,让我们说... |___O____________________| so that's how it'll work.所以这就是它的工作方式。 You basically press the left button to go left, right button to go right.你基本上按左键向左走,右键向右走。 Simple, or so I think.很简单,或者我认为是这样。

Here are my comments in a workable form in a jsfiddle:以下是我在 jsfiddle 中以可行形式发表的评论:

http://jsfiddle.net/yn2283c3/ http://jsfiddle.net/yn2283c3/

Other than some small changes I made to make the jsfiddle work (like adding jquery as a library because document.write isn't allowed in jsfiddle, and like making the click events be handled by jquery, and like adding <br /> tags at the end of each sequence of characters), it works as you described.除了我为使 jsfiddle 工作而进行的一些小更改(例如将 jquery 添加为库,因为 jsfiddle 中不允许使用 document.write,以及使单击事件由 jquery 处理,以及在以下位置添加<br />标签)每个字符序列的末尾),它按您的描述工作。

var FromStart = 0
document.write("|");
for (i = 0; i < FromStart; i++) {
    document.write("_");
}
document.write("O");
for (i = 0; i < 30-FromStart; i++) {
    document.write("_");
}
document.write("|<br />");

function Left() {
   FromStart += -1;
document.write("|");
for (i = 0; i < FromStart; i++) {
    document.write("_");
}
document.write("O");
for (i = 0; i < 30-FromStart; i++) {
    document.write("_");
}
document.write("|<br />");
}


function Right() {
FromStart += 1;
document.write("|");
for (i = 0; i < FromStart; i++) {
    document.write("_");
}
document.write("O");
for (i = 0; i < 30-FromStart; i++) {
    document.write("_");
}
document.write("|<br />");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM