首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript小游戏

Javascript小游戏
EN

Stack Overflow用户
提问于 2013-01-06 23:50:29
回答 1查看 1.8K关注 0票数 2

现在我正在学习一些Javascript,但现在有很多问题,因为我的技能很低。我需要一些帮助来解决代码中的几个问题。

我正在试着写一个叫“击鱼”的游戏。它有一个计时器,计分和onclick。只要点击一下,鱼就会消失,分数会增加1分。计时器限制为60秒。

下面是完整的代码。

代码语言:javascript
复制
<html>  
<head>  
    <title>  
        Hit the fish!
    </title>  
    <style>
        table{
            margin-left: auto;
            margin-right: auto;
            width: 70%;
            height: 90%;
            background-color:#66ff00;
        }
        #playground input{
            position: inherit;
            height: 100px;
            width: 100px;
            margin-left: 20px;
            margin-right: 20px;
            margin-bottom: 20px;
        }
        #input {
            height:40px;
        }
        #area {
            background-color:#888;
            position:absolute;
            left:0px;
            right:0px;
            top:50px;
            bottom:0px;
        }
        #area button {
            width:150px;
            height:30px;
            position:absolute;
        }
        .red {
            color:red;
        }

    </style>

    <script language="Javascript">
        function one () {
            document.play.one.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }

        function two () {
            document.play.two.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }

        function three () {
            document.play.three.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }

        function four () {
            document.play.four.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }

        function five () {
            document.play.five.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;

        }

        function six () {
            document.play.six.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }

        function seven () {
            document.play.seven.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }

        function eight () {
            document.play.eight.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }

        function nine () {
            document.play.nine.value="";
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }


        function count()
        {         
            stop();
            // get the counter element
            var counter = document.getElementById("counter");
            // get it's value
            var value = parseInt(counter.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            counter.innerHTML=value;
            // limitation
            if(value===60){
                alert("Time Out!");
                clearInterval(countTimer);
                document.getElementById("counter").innerHTML="0";
                document.getElementById("score").innerHTML="0";
            }
        }

        function start () {
            stop();
            var countTimer = setInterval("count()",1000);
            document.play.four.value=">(  °3°)";
            document.play.three.value=">(  °3°)"; 
            setTimeout("nextone ()");
            var score = document.getElementById("score");
        }
        function score(){
            // get the counter element
            var score = document.getElementById("score");
            // get it's value
            var value = parseInt(score.innerHTML);
            // increase it
            value = value + 1;
            // write the new value back
            score.innerHTML=value;
        }
        function nextone () {
            document.play.four.value="";
            document.play.five.value=">(  °3°)"; 

            setTimeout("nexttwo ()",400);
        }

        function nexttwo () {
            document.play.three.value=""; 
            setTimeout("nextthree()",400);
        }

        function nextthree () {
            document.play.seven.value=">(  °3°)";
            document.play.one.value=">(  °3°)";
            document.play.six.value=">(  °3°)"; 
            setTimeout("nextfour ()",700);
        }

        function nextfour () {
            document.play.one.value="";
            document.play.six.value="";
            document.play.two.value=">(  °3°)"; 
            setTimeout("nextfive ()",700);
        }

        function nextfive () {
            document.play.seven.value="";
            document.play.two.value="";
            document.play.four.value=">(  °3°)"; 
            setTimeout("nextsix ()",800);
        }

        function nextsix () {
            document.play.eight.value=">(  °3°)";
            document.play.two.value=">(  °3°)"; 
            setTimeout("nextseven ()",700);
        }

        function nextseven () {
            document.play.eight.value="";
            document.play.five.value=">(  °3°)"; 
            setTimeout("nexteight ()",400);
        }

        function nexteight () {
            document.play.nine.value=">(  °3°)"
            document.play.four.value=">(  °3°)"; 
            setTimeout("nextnine ()",500);
        }

        function nextnine () {
            document.play.five.value="";
            document.play.four.value="";
            document.play.one.value=">(  °3°)"; 
            setTimeout("nextten ()",200);
        }

        function nextten () {
            document.play.three.value=">(  °3°)";
            document.play.six.value=">(  °3°)"; 
            setTimeout("nexteleven ()",600);
        }

        function nexteleven () {
            document.play.one.value="";
            document.play.seven.value=">(  °3°)"; 
            setTimeout("nexttwelve ()",500);
        }

        function nexttwelve () {
            document.play.two.value=">(  °3°)";
            document.play.nine.value=">(  °3°)"; 
            setTimeout("nextthirteen ()",700);
        }

        function nextthirteen () {
            document.play.one.value=">(  °3°)";
            document.play.nine.value="";
            document.play.seven.value=""; 
            setTimeout("start ()",600);
        }

        function stop () {
            clearInterval(countTimer);
            document.play.one.value="";
            document.play.two.value="";
            document.play.three.value="";
            document.play.four.value="";
            document.play.five.value="";
            document.play.six.value="";
            document.play.seven.value="";
            document.play.eight.value="";
            document.play.nine.value="";
        }
        function reset()
        {
            document.getElementById("counter").innerHTML="0";
            document.getElementById("score").innerHTML="0";
        }

        // get the counter element
        var score = document.getElementById("score");
        // get it's value
        var value = parseInt(score.innerHTML);
        // increase it
        value = value + 1;
        // write the new value back
        score.innerHTML=value;


    </script> 
</head>  

<body>
    <div id="input">
        <button onclick="start()">start</button>
        <button onclick="stop()">stop</button>
        <button onclick="reset()">reset</button>
        <div style="font-size:10em" id="counter">0</div><br>
        <p>Your score:</p><div style="font-size:5em" id="score">0</div>
        <script>
            var countTimer = setInterval('count()',1000);
        </script>
    </div>
    <div id="playground">
        <table border=100 cellpadding=0 cellspacing=0>
            <tr>
                <td>
                    <form name="play">
                        <center>  
                            <INPUT TYPE="button" NAME="one" OnClick="one ()" id="one" value="      ">  
                            <INPUT TYPE="button" NAME="two" OnClick="two ()" id="two" value="      ">  
                            <INPUT TYPE="button" NAME="three" OnClick="three ()" id="three" value="      ">
                            <br>  
                            <INPUT TYPE="button" NAME="four" OnClick="four ()" id="four" value="      ">  
                            <INPUT TYPE="button" NAME="five" OnClick="five ()" id="five" value="      ">  
                            <INPUT TYPE="button" NAME="six" OnClick="six ()" id="six" value="      ">
                            <br>  
                            <INPUT TYPE="button" NAME="seven" OnClick="seven ()" id="seven" value="      ">  
                            <INPUT TYPE="button" NAME="eight" OnClick="eight ()" id="eight" value="      ">  
                            <INPUT TYPE="button" NAME="nine" OnClick="nine ()" id="ten" value="      ">
                            <br>
                            </td>
                            </tr>
                            </table>  
                            </div>
                            </body>  
                            </html>

问题是计数器在20 - 30之后走得太快了,点击按钮后鱼不会消失,分数也不会增加。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-07 03:43:11

没有特定的顺序。

(1)计数器的问题源于您调用它的方式以及存储countTimer变量的位置。因为您希望几个函数都能够访问countTimer,所以应该以所有函数都可以访问的方式声明它;最简单的方法是将其设置为全局变量,这只需使用

代码语言:javascript
复制
var countTimer;

在脚本的顶部,并且在其他地方引用countTimer时不使用var。从函数nextThirteen调用函数start,然后再次启动设置计时器。这实际上是在旧的计时器上设置一个新的计时器,这就是为什么计数似乎加快了。

(2)您的html也是无效的,可能会导致一些问题,请务必关闭formcenter标签。(不管怎样,你已经不再是not supposed to use the center tag了)。

(3)您的代码在head中,但在页面加载之前,大部分代码都会直接运行。因此,下面这行代码:

代码语言:javascript
复制
var score = document.getElementById("score");

(在任何函数之外)会导致错误,因为元素分数尚未写入。避免这种情况的最简单方法是将脚本放在页面主体的末尾。

(4)函数onetwothree需要与表单中的输入具有不同的名称。这就是为什么所有的按钮都不工作的原因。

当您了解更多信息时,请注意以下几点:

  • 避免重复。不需要所有的一、二、三个函数等等。你可以使用一个循环或单个事件处理程序来找出哪个按钮被按下了。
  • 使用浏览器的控制台来检查正在产生的错误。您的代码导致了错误object is not a function,而我正是通过searching for that找出了按钮不是关于函数的原因
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14183790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档