【问题标题】:javascript text input manipulationjavascript文本输入操作
【发布时间】:2015-09-24 16:47:26
【问题描述】:

我正在制作用于练习 javascript 的单词入侵游戏。 单词从顶部掉下来,应该输入丢弃单词并获得积分 但是,当我在输入框中输入字母时,我不能输入多个字母。 例如单词“汽车” 如果我输入“c”,然后输入“a”,c 就会消失。而且只有一个遗骸。 相关代码部分如下。只需忽略 listx,listy 部分。 我是否使用错误的输入类型文本或错误实现了某些内容?

<div id = 'input'>Input<input type = 'text' id = 'inputbox'></div>


function doKey(keyPressed) {
    if (window.event.keyCode === 13) {
        var submission = document.getElementById('inputbox').value;
        var result = checkLetter(submission);
    }
    //somethings here (unrelated)

    document.getElementById('inputbox').value = ''; //return to the first step
}

 function checkLetter (letter) {
    for(i = 0; i < alpha.length; i++) {
        if (letter == alpha[i]) {
            return i;
        }
    }
    return -1;
}

完整的代码在这里

<canvas id = "ctx" width = "500" height = "500" style = "border:1px solid #000000;"></canvas>
<h1>Typing Game</h1>

<div id = 'input'>Input<input type = 'text' id = 'inputbox'></div>
<div id = 'Score'>SCORE : <span id = 'score'>0</span></div>
<div id = 'LifePoint'>LIFE : <span id = 'life'>10</span></div>

<script>

var ctx = document.getElementById("ctx").getContext("2d");
ctx.font = '30px Arial'; //font should be fixed

var y = 10; //starting height

var spdY = 50;

var listx = [];
var listy = [];
var alpha = [];
var intervalTime = 800;



setInterval(update, intervalTime); //appearance interval

function doKey (keyPressed) {
    if (window.event.keyCode === 13) { //if pressed enter
        var submission = document.getElementById('inputbox').value;
        var result = checkLetter(submission);
        if (result > -1) {
            //value deletion
            alpha.splice(result, 1);
            //positions deletion
            listx.splice(result, 1);
            listy.splice(result, 1);
            increaseScore('score');
        } else {
            console.log("this is an error");
        }
    }
    document.getElementById('inputbox').value = ''; //return to the first step
}

function increaseScore(id) {
    var count = document.getElementById(id).innerHTML;
    count++;
    document.getElementById(id).innerHTML = count;
}

function decreaseScore(id) {
    var count = document.getElementById(id).innerHTML;
    count--;
    document.getElementById(id).innerHTML = count;
}

function lifeDeduction() {
    for (i=0; i<alpha.length; i++) {
        if (listy[i] > 500) {
            alpha.splice(i,1)
            listx.splice(i, 1);
            listy.splice(i, 1);
            decreaseScore('life');
        }
    }
}

function checkLetter (letter) {
    for(i = 0; i < alpha.length; i++) {
        if (letter == alpha[i]) {
            return i;
        }
    }
    return -1;
}

function getLetter() {
    var letters = ['apple', 'banana', 'color','door','egg','fraction','garlic','hello','icecream','junk','kawa'
    ,'lemon','monster','notorious','octopus','pure','qwerty','rabbit','strange','television','ultra','vex','window','xavi',
    'yellow','zzzzzzz']
    var i = Math.floor(Math.random()*27);
    var randomLetter = letters[i];
    alpha.push(randomLetter);
    var x = Math.floor((Math.random() * 450) + 1);
    listx.push(x);
    listy.push(y);
    //console.log(alpha);
}


function endoftheGame() {
    ctx.clearRect(0,0,500,500);
    ctx.fillText("END", 250, 250);
    ctx.fillText("Press R to restart", 250, 300);
}

function update() {
    if (document.getElementById('life').innerHTML == 0) {
        alpha = []
        listx = []
        listy = []
        console.log("fail");
        endoftheGame();
    } else {
        //console.log(alpha);
        //console.log(listx);
        //console.log(listy);
        spdY = 50 + 5*(document.getElementById('score').innerHTML/10);//speed
        lifeDeduction()
        getLetter();
        ctx.clearRect(0,0,500,500);
        draw();
    }
}

function draw() {
    for (i = 0; i < alpha.length; i++) {
        listy[i] += spdY;
        ctx.fillText(alpha[i],listx[i],listy[i]);
    }
}

document.getElementsByTagName("body")[0].onkeypress = doKey;


</script>

【问题讨论】:

  • 你可能想使用count = parseInt(document.getElementById(id).innerHTML,10)

标签: javascript input textinput


【解决方案1】:

这一行

document.getElementById('inputbox').value = ''; //return to the first step

在每次击键时清除您的输入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 2018-02-27
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 2019-02-02
    • 2011-02-15
    相关资源
    最近更新 更多