简体   繁体   English

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

[英]Why does my program stop working if I add a variable?

Why is it that if I remove var framesPerSecond , the program runs fine?为什么如果我删除var framesPerSecond ,程序运行良好? Even if I put the variable inside window.onload it doesn't work.即使我把变量放在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>

Ps.附言。 I am just a beginner.我只是一个初学者。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我添加 if 语句,为什么我的 jQuery 自动刷新停止工作? - why does my jQuery auto-refresh stop working if I add in a if statement? 如果我使用有错误的代码,为什么我的下拉菜单停止工作? - Why does my dropdown stop working if I use code with errors? 删除静音后,为什么我的自动播放停止工作? - Why does my autoplay stop working when I remove mute? 为什么我的 typescript 程序随机停止运行? - Why does my typescript program randomly stop running? 将不相关的div添加到DOM后,为什么JQuery UI Datepicker停止工作? - Why does JQuery UI Datepicker stop working after I add an unrelated div to the DOM? 当我添加另一个时,为什么我的第一个javascript会停止? - Why does my first javascript stop when I add another one? 添加setTimeout后,为什么jQuery函数在“ else if”处停止运行? - why does my jQuery function stop running at “else if” after I add a setTimeout? 将jquery插件放入div容器后,为什么我的jquery插件停止工作? - Why does my jquery plugin stop working after I put it in a div container? 为什么我的代码在几个循环后停止工作? - Why does my code stop working after few loops? 为什么我的 barbajs 转换在与 parcel 捆绑在一起时停止工作? - Why does my barbajs transitions stop working when bundled with parcel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM