2

現在、Javascript をいくつか学んでいますが、スキルが低いため、多くの問題が発生しています。このコードのいくつかの問題について、助けが必要です。

「hit the fish」というゲームを書こうとしています。タイマー、スコア、onclick があります。クリックすると魚が消え、スコアに 1 ポイントが追加されます。60秒のタイマー制限があります。

これがコード全体です。

<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 を過ぎると速すぎて、ボタンをクリックしても魚が消えず、スコアにポイントが追加されないことです。

4

1 に答える 1

2

順不同で・・・

(1) カウンターの問題は、それを呼び出す方法とcountTimer変数を格納する場所が原因です。いくつかの関数が にアクセスできるようにするためcountTimer、すべての関数がアクセスできるように宣言する必要があります。最も簡単な方法は、それをグローバル変数にすることです。これは、

var countTimer;

スクリプトの先頭に置き、他の場所を参照するときは使用しません。functionから関数を呼び出し、start でタイマーを再度設定します。これが実際に行うことは、古いタイマーの上に新しいタイマーを設定することです。これが、カウントがスピードアップしているように見える理由です。varcountTimerstartnextThirteen

(2) あなたの html も有効ではないため、問題が発生する可能性があります。formcenterタグを必ず閉じてください。(とにかく、タグをもう使用することは想定されていません)。center

(3) コードは にありますが、headそのほとんどは、ページが読み込まれる前にすぐに実行されます。したがって、次の行:

var score = document.getElementById("score");

(どの関数の外側でも) 要素スコアがまだ書き込まれていないため、エラーが発生します。これを回避する最も簡単な方法は、スクリプトをページの本文の最後に配置することです。

(4) 関数onetwothreeは、フォームの入力とは異なる名前にする必要があります。そのため、どのボタンも機能しません。

あなたがより多くを学ぶにつれて、いくつかの一般的なポイント:

  • 繰り返しを避ける。1 つ、2 つ、3 つの関数などをすべて使用する必要はありません。どのボタンが押されたかを検出するループまたは単一のイベント ハンドラーを使用できます。
  • ブラウザのコンソールを使用して、発生しているエラーを確認してください。あなたのコードはエラーobject is not a functionを引き起こしていました。それを検索すると、ボタンが機能しない理由がわかりました。
  • 変数のスコープについて読む
于 2013-01-06T19:43:10.583 に答える