gpt4 book ai didi

javascript 文本输入操作

转载 作者:行者123 更新时间:2023-12-03 09:38:09 28 4
gpt4 key购买 nike

我正在制作文字入侵游戏来练习 javascript。单词从顶部落下,应该输入以丢弃单词并获得分数但是,当我在输入框中输入字母时,我无法输入多个字母。例如单词“car”如果我输入“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>

最佳答案

这一行

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

每次击键时都会清除您的输入

关于javascript 文本输入操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31264478/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com