如果我添加变量,为什么我的程序会停止工作?

时间:2018-02-04 11:06:45

标签: javascript html debugging

为什么如果删除var framesPerSecond,程序运行正常?即使我把变量放在window.onload里面也不行。

<canvas id="gameCanvas" width="800" height="600"></canvas>

<script>
var canvas;
var canvasContext;
var ballX = 50;
var ballSpeedX = 5;
var framesPerSecond = math.random()*(100-10)+10;
window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
setInterval(callBoth, framesPerSecond);
}

function callBoth() {
  moveEverything();
  drawEverything();
}

function moveEverything() {
  ballX = ballX + ballSpeedX;
  if(ballX > canvas.width) {
    ballSpeedX = -ballSpeedX;
  }
  if(ballX < 0) {
    ballSpeedX = -ballSpeedX;
  }
}

function drawEverything() {
// canvas
  colorRect(0,0,canvas.width,canvas.height,'black');
// obstacles
  colorRect(40,40,720,520,'blue');
// car
  colorRect(ballX,100,10,10,'white');
}

function colorRect(leftX, topY, width, height, drawColor) {
  canvasContext.fillStyle = drawColor;
  canvasContext.fillRect(leftX, topY, width, height);
}
</script>

聚苯乙烯。我只是个初学者。

1 个答案:

答案 0 :(得分:1)

Math应该大写: var framesPerSecond = Math.random()*(100-10)+10;