2014-10-17 67 views
-2

我一直在尝试使用HTML5和Javascript将简单的迷宫游戏放在一起。我可以成功地将HTML和CSS内容加载到页面上,但无论我尝试什么,我都无法加载JS。它绝对保存为.html文件,我只使用Sublime文本将它放在一起(但我不会认为这会影响到它)。只是有点难倒了,所以我认为这一定是我在代码中遗漏的东西。我不确定我是否错过了一些东西?Javascript未加载HTML

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8" /> 
<title> Maze Game </title> 
</head> 
<style> 
canvas { 
border: 8px double navy; 
background: white; 
} 

img { 
    display: none; 
} 

button { 
padding: 3px; 
} 
</style> 
<body> 
<canvas id="canvas"> </canvas> 
<img id="sprite" src="sprite.png"> 
<script> 
//these define the global variables for the canvas and the drawing context 
var canvas; 
var context; 

var x = 0; 
var y = 0; //positioning of the sprite 

var dx = 0; 
var dy = 0; //momentum of the sprite at start 

window.onload = function() { 
    //setting up the canvas 
    canvas = document.getElementById("canvas"); 
    context = canvas.getContext("2d"); 
    //Draws the maze background 
    drawMaze("maze.png", 268, 5); 

    //On key press, run the following function 
    window.onkeydown = processKey; 
}; 


var x = 0; 
var y = 0; 

function drawMaze(mazeFile, Xstart, Ystart) { 
    //This loads the maze picture in 
    dx = 0; 
    dy = 0; //if the face is already moving, stop it 
    var imgMaze = new Image(); 
    imgMaze.onLoad = function() { 

     canvas.width = imgMaze.width; 
     canvas.height = imgMaze.height; 

     //Draws the maze onto the canvas 
     context.drawImage(imgMaze, 0, 0); 

     //draws the sprite and positions 
     x = Xstart; 
     y = Ystart; 
     var imgSprite = document.getElementById("sprite"); 
     context.drawImage(imgSprite, x, y); 
     context.stroke(); 

     //sets a short timer for the next frame to be drawn in (10ms) 
     setTimeout("drawFrame()", 10); 
    }; 
    imgMaze.src = mazeFile; 
} 

function processKey(e) { //e needs to be used for event handling 
    //stop the sprite if it's already moving - enables collision 
    var dx = 0; 
    var dy = 0; 

    //condition for the Up arrow being pressed 
    if (e.keyCode == 38) { 
     dy = -1; 
    } 

    //condition for the Left arrow being pressed 
    if (e.keyCode == 37) { 
     dx = -1; 
    } 

    //condition for the Down arrow being pressed 
    if (e.keyCode == 40) { 
     dy = 1; 
    } 

    //condition for the Right arrow being pressed 
    if (e.keyCode == 39) { 
     dx = 1; 
    } 

} 

function drawFrame() { 
    if (dx != 0 || dy != 0) { 
     context.beginPath(); 
     context.fillStyle = "rgb(254,244,207)"; 
     context.rect(x, y, 15, 15); 
     context.fill 


     x += dx; 
     y += dy; 

     if (checkForCollision()) { 
(dx/y = 0) 
      x -= dx; 
      y -= dy; 
      dx = 0; 
      dy = 0; 
     } 
     //Now we can finally draw the sprite! 

     var imgSprite = document.getElementById("sprite"); 
     context.drawImage(imgSprite, x, y); 



     if (y > (canvas.height - 17)) { 
      alert("Congratulations! You made it!"); 
      return; 
     } 
    } 

    timer = setTimeout(drawFrame, 10); 
} 

var imageData = context.getImageData(0, 0, 100, 50); 
var pixels = imageData.data; 


for (var i = 0, n = pixels.length; i < n; i += 4) { 
//This will get the data/values for one pixel 
var red = pixels[i]; 
var green = pixels [i+1]; 
var blue = pixels [i+2]; 
var alpha = pixels [i+3]; 

//This will invert the colours 
pixels[i] = 255 - red; 
pixels[i+1] = 255 - green; 
pixels[i+2] = 255 - blue; 
} 
context.putImageData(imageData, 0, 0); 


function checkForCollision() { 

var imgData = context.getImageData(x-1, y-1, 15+2, 15+2); 
var pixels = imgData.data; 

//Then we need to perform a check, same as above 
for (var i = 0; n = pixels.length, i < n; i += 4) { 
    var red = pixels[i]; 
    var green = pixels[i+1]; 
    var blue = pixels[i+2]; 
    var alpha = pixels[i+3]; 
    //now check for the black pixels for a wall 
    if (red == 0 && green == 0 && blue == 0) { 
     return true; 
    } //checks for a greyish colour - possibly the edge of a wall 
    if (red == 169 && green == 169 && blue == 169) { 
     return true; 
    } 
} 
return false; //there was no collision 
} 

</script> 
</body> 
</html> 
+2

“我无法获得JS加载”是什么意思?控制台中是否有任何错误(F12)?你有没有做过任何基本的调试? – Andy 2014-10-17 10:35:14

+1

您是在本地运行代码还是从服务器运行代码?你正在使用哪种浏览器?开发人员工具中是否显示任何错误? – 2014-10-17 10:35:24

+2

其实你有一个错误。第115行:ReferenceError:无效赋值左边:表示“(dx/y = 0)”的行。 – Andy 2014-10-17 10:36:32

回答

0

这段代码有很多错误。例如,仅在这一节:

(评论这里有些问题)

function drawFrame() { 
    if (dx != 0 || dy != 0) { 
     context.beginPath(); 
     context.fillStyle = "rgb(254,244,207)"; 
     context.rect(x, y, 15, 15); 
     context.fill     // Missing parentheses and semicolon 


     x += dx; 
     y += dy; 

     if (checkForCollision()) { 
(dx/y = 0)      // Equivalent to { dx/(y = 0) } 
      x -= dx;    // which both serves no purpose and divides by zero 
      y -= dy; 
      dx = 0; 
      dy = 0; 
     } 
     //Now we can finally draw the sprite! 

     var imgSprite = document.getElementById("sprite"); 
     context.drawImage(imgSprite, x, y); 



     if (y > (canvas.height - 17)) { 
      alert("Congratulations! You made it!"); 
      return; 
     } 
    } 

    timer = setTimeout(drawFrame, 10);  // timer is not defined anywhere 
              // also you are calling the function within itself 
              // with no end condition, so it's an infinite loop 
} 

和在线48:

setTimeout("drawFrame()", 10); 

你应该传递函数那样的功能标识,不是一个字符串。因此:

setTimeout(drawFrame, 10); 

这些只是一些。还有一些逻辑错误,定义但从未使用过的变量等等。在目前的状态下,这不会被编译。

而且,如果仅仅只是为了清楚起见,尝试定义的脚本类型,而不是只留下标记为空,如:

<script type="text/javascript"> 
// Some JS 
</script> 

这时候,你已经在代码盯着几个小时能吃苦耐劳,但是它有助于为每个部分提供缓慢的通读,并仔细考虑代码的作用。您可以通过这种方式避免大量的语法和逻辑错误。

我还推荐使用具有JShint/lint或某种代码检查功能的文本编辑器或在线界面(JS Bin,JSfiddle等)。你甚至可以使用http://www.javascriptlint.com/online_lint.php并粘贴你的整个代码。

+0

谢谢!我删除了并做了一个文本比较工具。看起来现在工作。 – user3293367 2014-10-17 13:53:51