2014-10-17 104 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