【发布时间】:2016-08-28 06:59:30
【问题描述】:
我在第 126 行的 JS/HTML5 pong 游戏项目中收到错误:
tennisJS_game.html:126 Uncaught SyntaxError: Unexpected end of input
我看不出哪里出错了。我关闭了函数,其他一切看起来都很好。
下面是我的代码
var canvas;
var canvasContext;
var ballX = 50;
var ballY = 50;
var ballSpeedX = 10;
var ballSpeedY = 4;
var paddle1Y = 250;
const PADDLE_HEIGHT = 100;
// C Programming #define (ALWAYS CONSTANT)
/*
Observing returning event (evt) of mouse positioning coordinates in relation to where the mouse is on the "gameCanvas"
For example starting point on the "gameCanvas" is on (0,0) on the (y,x) axis
EventListener wouldn't care about mouse position or "mousePos" if this code wasn't here
Two piece of data are being returned here... mouseX and mouseY
*/
function calculateMousePos(evt) {
var rect = canvas.getBoundingClientRect();
var root = document.documentElement;
var mouseX = evt.clientX - rect.left - root.scrollLeft;
var mouseY = evt.clientY - rect.top - root.scrollTop;
return {
x:mouseX,
y:mouseY
};
}
//variables section
//Speed of objects & EventListener & Resetting the Ball section
window.onload = function () {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
var framesPerSecond = 30;
setInterval(function() {
moveEverything();
drawEverything();
}, 1000/framesPerSecond );
canvas.addEventListener('mousemove',
function(evt) {
var mousePos = calculateMousePos(evt);
paddle1Y = mousePos.y-(PADDLE_HEIGHT/2); //So players mouse and paddle relationship position is center
});
}
function moveEverything() {
ballX = ballX + ballSpeedX;
ballY = ballY + ballSpeedY
if(ballX < 0) {
if(ballY > paddle1Y &&
ballY < paddle1Y+PADDLE_HEIGHT) {
ballSpeedX = -ballSpeedX;
} else {
ballReset(); }
if(ballX > canvas.width) {
ballSpeedX = -ballSpeedX;
}
if(ballY < 0) {
ballSpeedY = -ballSpeedY;
}
if(ballY > canvas.height) {
ballSpeedY = -ballSpeedY;
}
//Speed of objects & EventListener & Resetting the Ball section
function colorRect(leftX,topY,width,height,drawColor) {
canvasContext.fillStyle = drawColor;
canvasContext.fillRect(leftX,topY, width, height);
}
//Ball Reset f(x) section
function ballReset() {
ballSpeedX = -ballSpeedX;
ballX = canvas.width/2;
ballY = canvas.height/2;
}
//Ball Reset f(x) section
//canvas arguments/parameters
function drawEverything () {
// next line blanks out the screen with black
colorRect(0,0,canvas.width,canvas.height,'black');
// this left player paddle
colorRect(0,paddle1Y,10,100,'white');
// next line draws the ball
colorCircle(ballX,ballY,10,'white');
}
function colorCircle(centerX,centerY,radius, drawColor) {
canvasContext.fillStyle = drawColor;
canvasContext.beginPath();
canvasContext.arc(centerX,centerY,radius,0,Math.PI*2,true);
canvasContext.fill();}
//canvas arguments/parameters
【问题讨论】:
-
函数
moveEverything的括号没有正确关闭。 -
对于
if(ballX < 0) {,也没有右括号。 -
只需将两个右大括号
}}放在文件末尾 -
@JeremyRedkey。由于您的问题已解决,您可以将解决方案作为答案发布,然后接受您自己的答案。或删除您的问题。这两个选项中的任何一个都会将您的问题从“未回答的问题”队列中删除。谢谢! :-)
-
第一种方法是使用一个为您突出显示错误和缩进的编辑器。你这样做吗?如果做不到这一点,经典的方法是“分而治之”——不断删除部分代码,直到找到导致问题的部分。如果你打算做大量的 JS 编程(或任何语言的编程),你应该优先学习这两种方法,因为你不能每次打错字都发到 SO。
标签: javascript node.js html canvas javascript-objects