[英]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.