简体   繁体   English

儿童的Javascript游戏

[英]Javascript Game for Kids

I'm trying to make a game in javascript. 我正在尝试用javascript制作游戏。 I still have many issues in the code. 我的代码中仍然有很多问题。 The code is supposed to have 2 buttons; 代码应该有2个按钮; one for shuffling images dice and a button to check if the kid wrote the correct answer or not. 一个用于洗牌图像骰子和一个按钮,以检查孩子是否写了正确的答案。

  • 2 buttons 2个按钮
  • 1 textbox 1个文本框
  • and a place to show the answer if it's correct or not 如果正确与否,还有一个显示答案的地方

Here is my code. 这是我的代码。

I don't know why when I put the source of document.getElementbyID("test") it shows nothing because I want every time I click on start a random image is selected. 我不知道为什么当我把document.getElementbyID(“test”)的来源放在什么时候什么都没有,因为我希望每次点击开始随机图像都被选中。

I would appreciate any help as I am still a beginner in javascript. 我很感激任何帮助,因为我仍然是javascript的初学者。

  <head> 

<script type="text/javascript">


function startf(){
var images = []; 
index = 0;
images[0] = "<img src='1.png' length=70px width=75px>";
images[1] = "<img src='2.png' length=70px width=75px>";
images[2] = "<img src='3.png' length=70px width=75px>";
images[3] = "<img src='4.png' length=70px width=75px>";
index = Math.floor(Math.random() * images.length);
take(index);

function take(ind)
{
return document.getElementbyId("ind")="What should i put here";
}
}
function check(){
var ch=getElementbyId("answer").value;
if (ch=index+1)
{
document.getElementbyId.innerHTML="TRUE";
}
else
{
document.getElementbyId.innerHTML="False";
}

}
</script><br>
</head>
<img id="test" src=""  alt="..." length="75px" width="75px" />

<body>

<input type="button" value="start" onclick="startf()">
<input id="answer" type="text" name="checkvalue" value="Enter Value" onclick="check()">
<div id="fa">
</div>
<input type="button" value=chek onclick="check()">


</body>

1- Put and end to each instructions --> ; 1-放置和结束每条指令 - > ;

2- Do not use document.getElementById directly, there will be at least one occurence with an error into it and you don't want that. 2-不要直接使用document.getElementById,至少会出现一个错误,并且你不想这样做。

function _e(id) {
   return document.getElementById(id);
}

3- Always put brackets and (...) around your IF-ELSE blocks: 3-始终在IF-ELSE块周围放置括号和(...):

if (...) {
    //....
} else {
    //....
}

4- Every tag attributes should have " " around their values, for example: 4-每个标记属性的值都应该是“”,例如:

<input type="button" value="start" onclick="check()" />

5- You could only put image paths in your array since it seems to be what needs to be updated in #test image. 5-您只能在阵列中放置图像路径,因为它似乎需要在#test图像中更新。

It is document.getElementById check the casing. 它是document.getElementById检查外壳。 Your check function is wrong... you can't assign a value to document.getElementById function. 您的检查功能错误...您无法为document.getElementById函数赋值。 Also your if is wrong. 你的if也错了。 Do you know any JavaScript? 你知道任何JavaScript吗?

I'm guessing what you want is probably something like this. 我猜你想要的东西可能是这样的。 You seem to be trying to add or replace an element just by using document.getElementById(id) = something but that's not how it works. 您似乎只是尝试使用document.getElementById(id) = something来添加或替换元素,但这不是它的工作方式。 Instead, to change an image to another file you need to alter its src attribute. 相反,要将图像更改为另一个文件,您需要更改其src属性。 (There are other ways but this is maybe the simplest.) (还有其他方法,但这可能是最简单的。)

// Declare the variable called number here so it can be accessed
// by all of the following functions.
var number;

// a simple function to save typing document.getElementById all the time
function _e(id) {
    return document.getElementById(id);
}
function startf() {
    // Pick a number between 1 and 4
    number = Math.floor(Math.random() * 4 + 1);

    // Set the image with id 'test' to have the source 1.png, 2.png etc.
    _e('test').src = number + '.png';
}

function check() {
    // note that == is used for comparison, = is used for assignment
    if (_e('answer').value == number) {
        _e('fa').innerHTML = "TRUE";
    }
    else {
        _e('fa').innerHTML = "False";
    }
}​

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

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