2017-06-03 2 views
-1

Dies ist mein Code und wenn statt einer schwarzen Box mit meinem Spiel in der Mitte des Bildschirms ausgeführt wird, gibt es nur eine Lücke und keine Website kann scheinen mir zu sagen, wo ich schief gelaufen ist:Mein Javascript gibt keine Ausgabe und wenn ich die Seite neu lade stürzt es

console.log("start") 
 
//Global functions 
 
var canvas = document.getElementById("canva"); 
 
var canvasCon; 
 
var snakeX = 400; 
 
var snakeY = 300; 
 
var direction; 
 
var snakeSpeed = 1; 
 
var apple = false; 
 
var appleX; 
 
var appleY 
 
var snakeSize = 30; 
 
var scoreboard = document.getElementById("score"); 
 
var Score = 0; 
 
var trailX = new Array(); 
 
var trailY = new Array(); 
 

 
console.log("set global variables!") 
 

 
window.onload = function(){ 
 
    console.log("ran on window load function") 
 
    canvasCon = canvas.getContext('2d'); 
 

 
    //Calls "drawing" and "move" fps times per second 
 
    var fps = 30 
 
    setInterval(function(){ 
 
     move() 
 
     drawing() 
 
    },1000/fps); 
 
} 
 

 
document.addEventListener('keydown', getKey); 
 
function getKey(event){ 
 
    //gets the keycode 
 
    direction = event.keyCode; 
 
    return direction; 
 
    } 
 

 
//moves the objsects 
 
function move(){ 
 
    //creates a easier to read way to check the value of getKey 
 
    var left = 37; 
 
    var up = 38; 
 
    var right = 39; 
 
    var down = 40; 
 

 

 

 
    //changing the position of the snake 
 
    switch(direction){ 
 
     case right: 
 
      snakeX = snakeX + snakeSpeed; 
 
      break; 
 
     case left: 
 
      snakeX = snakeX - snakeSpeed; 
 
      break; 
 
     case up: 
 
      snakeY = snakeY - snakeSpeed; 
 
      break; 
 
     case down: 
 
      snakeY = snakeY + snakeSpeed; 
 
      break; 
 
    } 
 
    trail() 
 
    //checks if snake has gone off the board and then moves the snake to the other end of the board if that returns True   
 
    if(snakeX > canvas.width){ 
 
     snakeX = 0 
 
    }else if(snakeX < 0){ 
 
     snakeX = canvas.width 
 
    } 
 

 
    if(snakeY > canvas.height){ 
 
     snakeY = 0 
 
    }else if(snakeY < 0){ 
 
     snakeY = canvas.height 
 
    } 
 
} 
 

 
//draws everything /w updated coordinates 
 
function drawing(){ 
 

 
    //Backround of the Game 
 
    canvasCon.fillStyle = 'black'; 
 
    canvasCon.fillRect(0,0,canvas.width,canvas.height); 
 

 
    //The snake 
 
    canvasCon.fillStyle = 'green'; 
 
    canvasCon.fillRect(snakeX,snakeY,snakeSize,snakeSize); 
 

 
    //apple creation 
 
    apples() 
 
    canvasCon.fillStyle = 'red'; 
 
    canvasCon.fillRect(appleX,appleY,25,25); 
 
} 
 

 
//makes the apples work 
 
function apples(){ 
 
    if(collision()){ 
 
     Score += 1 
 
     console.log(Score) 
 
     scoreboard.innerHTML = "<b>Score: "+Score+"<b>" 
 
     apple = false 
 
    } 
 
    if(apple == false){ 
 
     apple = true 
 
     appleX = Math.floor(Math.random() * (canvas.width - 50)); 
 
     appleY = Math.floor(Math.random() * (canvas.height - 50)); 
 
    } 
 

 
} 
 
function collision(){ 
 
    var distX = Math.abs(appleX - snakeX-snakeSize/2); 
 
    var distY = Math.abs(appleY - snakeY-snakeSize/2); 
 
    if (distX <= (snakeSize/2) && distY <= (snakeSize/2)) { return true; } 
 
} 
 
function trail(){ 
 
    //adds X and Y coordinates to their respective lists 
 
    trailX.push(snakeX) 
 
    trailY.push(snakeY) 
 
    //makes sure the lists are the right length 
 
    while(trailX.length > Score){ 
 
     var index = trailX.indexOf(0); 
 
     trailX.splice(index, 1); 
 
    } 
 
    while(trailY.length > Score){ 
 
     var index = trailY.indexOf(0); 
 
     trailY.splice(index, 1); 
 
    } 
 
    if(trailY.length != trailX.length){ 
 
     console.log("trailY != trailX") 
 
    } 
 
    for(i= trailX.length-1; i<=0; i--){ 
 
     canvasCon.fillStyle = 'yellow'; 
 
     canvasCon.fillRect(trailX[i],trailY[i],25,25); 
 
    } 
 
}
<title>SNAKE!!</title> 
 

 
<body> 
 
<p style="text-align:center; text-color:red"><b>Hope you Enjoy Ma GREAT GAME!!</b></p> 
 
<br/><br/><br/> 
 
<div style="width:800px; margin:0 auto;"> 
 
<canvas id=canva width="800" height="600"></canvas> 
 
<br/> 
 
<p id="score">Score: </p> 
 
</div> 
 
</body>

, wenn die Seite neu geladen wird sogar auf jsfiddle.net stürzt. plz Hilfe Ich habe keine Ahnung, was ich falsch gemacht habe!

+0

Haben Sie einen Debugger verwendet genau auf die Spur, wo es abstürzt? – RJM

+0

Höchstwahrscheinlich eine Endlosschleife, das sind die klassischen Täter, die eine Seite zum Absturz bringen/einfrieren – Lixus

+0

Ich weiß nicht, ob es die Ursache des Problems ist, aber Sie sollten keine Animationen mit 'setInterval' programmieren. Moderne Browser haben dafür [requestAnimationFrame] (https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) –

Antwort

4

Erste Regel von JS - wenn es fest ist, haben Sie wahrscheinlich eine Endlosschleife.

Und in der Tat Sie haben eine Endlosschleife mit Ihrem letzten für:

for(i= trailX.length-1; i<=0; i--){ 
     canvasCon.fillStyle = 'yellow'; 
     canvasCon.fillRect(trailX[i],trailY[i],25,25); 
    } 

i < = 0

Wenn i mit 0 und geht nach unten um 1 bei jeder Iteration zu starten, wird es immer kleiner oder gleich 0, damit die Endbedingung nie erfüllt wird.

Verwandte Themen