调试:第126行:语法错误意外结束

时间:2016-08-28 06:59:30

标签: javascript node.js html5 canvas javascript-objects

我在第126行的JS / HTML5乒乓游戏项目中收到错误:

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

1 个答案:

答案 0 :(得分:0)

功能移动的支架一切都没有正确关闭。 - Nehal J Wani 8月28日7:06

if if(ballX&lt; 0){没有关闭括号。 - Nehal J Wani 8月28日7:06