我的HTML游戏没有运行,出了什么问题?

时间:2015-11-30 11:41:02

标签: javascript html css

我从在线阅读的教程中写了这个游戏,它工作得很好,直到我搞砸了,我的开发人员工具控制台说这是一个参考错误,但我无法看到它,请帮助,我得到的错误我的开发者控制台是" Uncaught ReferenceError:Draw未定义(匿名函数)@ games.html:228"

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Gamedev Canvas Workshop</title>
        <style>
            * { padding: 0; margin: 0; }
            canvas { background: #eee; 
            display: block; 
            margin: 0 auto; 
            }
            #checking {
            margin-left:650px;
            }
            #checking1 {
            margin-left:650px;
            }
            #checking2 {
            margin-left:650px;
            }
            #checking3 {
            margin-left:650px;
            }
        </style>
    </head>
    <body>
        <canvas id="myCanvas" width="480" height="320"></canvas>
        <br/>
        <div id ="checking">Current Position</div>
        <div id ="checking1">Nothings Moving Bro</div>
        <br/>
        <div id ="checking3">Color State</div>
        <div id ="checking2">Nope, still nothing</div>
        <script>
            // JavaScript code goes here
               var checking1 = document.getElementById("checking1");
               var canvas = document.getElementById("myCanvas");
               var ctx = canvas.getContext("2d");

               var x = canvas.width/2;
               var y = canvas.height-30;
               var ballRadius = 20;  
               var dx = 2;
               var dy = -2;
               var paddleheight = 10;
               var paddlewidth = 50;
               var paddleX = (canvas.height - paddlewidth)/2;
               var RightKeyPressed = false;
               var LeftKeyPressed = false;


               document.addEventListener("keydown", keyDownHandler,false);
               document.addEventListener("keyup",keyUpHandler,false);


               function keyDownHandler(e){

                   if(e.keycode == 39){

                       RightKeyPressed = true;


                   }else if(e.keycode == 37){

                       LeftKeyPressed = true;


                   } 
               }


               function keyUpHandler(e){

                   if(e.keycode == 39){


                       RightKeyPressed = false;



                   }else if(e.keycode == 37){

                       LeftKeyPressed = false;


                   }    

                function randomInt(min,max){

                       return Math.floor(Math.random() * (max - min))+ min; }


               function paddle(){

                   ctx.beginPath;
                   ctx.rect(paddleX,canvas.height - paddleheight,paddlewidth,paddleheight);
                   ctx.fillStyle="#0095DD";
                   ctx.fill();
                   ctx.closePath;






               }


               function drawball(){
                    ctx.clearRect(0,0,canvas.width,canvas.height);
                    ctx.beginPath();
                    ctx.arc(x,y,ballRadius,0,Math.PI*2,false);
                    ctx.fillStyle = "rgb(0,149,221)";
                    ctx.fill();
                    ctx.closePath();




                 }


               function Draw(){

                   ctx.clearRect(0,0,canvas.width,canvas.height);


                   drawball();
                  paddle();









                   if(y + dy > canvas.height - 20 || y + dy < ballRadius){


                       var cx =  randomInt(0,255);
                       var cy = randomInt(0,255);
                       var cz = randomInt(0,255);


                           ctx.fillStyle = "rgb(cx,cy,cz)";
                           ctx.fill();



                       dy = -dy;



                   }



               if ( x + dx > canvas.width - 20  || x + dx < ballRadius){


                   var cx =  randomInt(0,255);
                       var cy = randomInt(0,255);
                       var cz = randomInt(0,255);


                           ctx.fillStyle = "rgb(cx,cy,cz)";
                           ctx.fill();  


                   dx = -dx; }



                    x += dx;
                    y += dy

                    var color = ctx.fillStyle;
                    checking1.innerHTML = " x: " + x + "y: " + y ;
                    checking2.innerHTML = color ;


               }







               }


               if(RightKeyPressed && paddleX < canvas.width - 50 ){

                   paddleX += 7;

               }else if(LeftKeyPressed && paddleX > 0){

                   paddleX -= 7;







               }


               setInterval(Draw,10);






        </script>
    </body>
</html>

3 个答案:

答案 0 :(得分:4)

您的代码会抛出错误,因为找不到函数Draw()

setInterval(Draw, 10);

这是因为您的功能不在同一范围内。你的代码结构是这样的:

function keyUpHandler(e) {
   ...
   function Draw(){
      //Codes here
   }
   ...
}
...
setInterval(Draw, 10);

由于Draw()函数位于keyUpHandler(e)内部,因此无法从任何外部函数(即更高级别/范围的函数)中看到它。

希望这能让你大吃一惊:)

答案 1 :(得分:0)

你可能忘了关闭你的keyuphandler函数(目前你在它里面有3个函数,直到最后在第193行关闭它)。

在第88行附近添加一个函数闭包来关闭你的keyuphandler。

在第193行你有一个额外的函数闭包删除它并且它可以工作

(通过函数闭包,我的意思是括号{})

答案 2 :(得分:0)

看看这个fiddle,你关闭函数keyDownHandlercode的括号错误

 var checking1 = document.getElementById("checking1");
 var canvas = document.getElementById("myCanvas");
 var ctx = canvas.getContext("2d");

 var x = canvas.width / 2;
 var y = canvas.height - 30;
 var ballRadius = 20;
 var dx = 2;
 var dy = -2;
 var paddleheight = 10;
 var paddlewidth = 50;
 var paddleX = (canvas.height - paddlewidth) / 2;
 var RightKeyPressed = false;
 var LeftKeyPressed = false;


 document.addEventListener("keydown", keyDownHandler, false);
 document.addEventListener("keyup", keyUpHandler, false);


 function keyDownHandler(e) {

     if (e.keycode == 39) {

         RightKeyPressed = true;


     } else if (e.keycode == 37) {

         LeftKeyPressed = true;


     }
 }


 function keyUpHandler(e) {

     if (e.keycode == 39) {


         RightKeyPressed = false;



     } else if (e.keycode == 37) {

         LeftKeyPressed = false;


     }
 }
     function randomInt(min, max) {

         return Math.floor(Math.random() * (max - min)) + min;
     }


     function paddle() {

         ctx.beginPath;
         ctx.rect(paddleX, canvas.height - paddleheight, paddlewidth, paddleheight);
         ctx.fillStyle = "#0095DD";
         ctx.fill();
         ctx.closePath;
     }


     function drawball() {
         ctx.clearRect(0, 0, canvas.width, canvas.height);
         ctx.beginPath();
         ctx.arc(x, y, ballRadius, 0, Math.PI * 2, false);
         ctx.fillStyle = "rgb(0,149,221)";
         ctx.fill();
         ctx.closePath();
     }

     function zan(){
     alert('test');
     }

     function Draw() {

         ctx.clearRect(0, 0, canvas.width, canvas.height);
         drawball();
         paddle();
         if (y + dy > canvas.height - 20 || y + dy < ballRadius) {
             var cx = randomInt(0, 255);
             var cy = randomInt(0, 255);
             var cz = randomInt(0, 255);
             ctx.fillStyle = "rgb(cx,cy,cz)";
             ctx.fill();
             dy = -dy;
         }
         if (x + dx > canvas.width - 20 || x + dx < ballRadius) {
             var cx = randomInt(0, 255);
             var cy = randomInt(0, 255);
             var cz = randomInt(0, 255);
             ctx.fillStyle = "rgb(cx,cy,cz)";
             ctx.fill();
             dx = -dx;
         }
         x += dx;
         y += dy

         var color = ctx.fillStyle;
         checking1.innerHTML = " x: " + x + "y: " + y;
         checking2.innerHTML = color;
     }



 if (RightKeyPressed && paddleX < canvas.width - 50) {
     paddleX += 7;
 } else if (LeftKeyPressed && paddleX > 0) {
     paddleX -= 7;
 }

 setInterval(Draw, 10);