我正在尝试通过创建一些经典的街机游戏来学习javascript,在测试我的游戏时,我收到了一个未捕获的语法错误:来自Chrome控制台的意外令牌功能,无法弄清楚如何调试它。 以下是错误所涉及的代码:
function drawEverything() {
//Fills the screen
colorRect(0, 0, canvas.width, canvas.height, 'black');
if(showingLoseScreen) {
canvasContext.font = '16px Arial';
canvasContext.fillStyle = 'white';
canvasContext.fillText("You lost!", 395, 200);
canvasContext.fillText("Score :" + playerScore, 380, 300);
canvasContext.fillText("Total Score :" + totalScore, 380, 350);
canvasContext.fillText("click to reload game", 380, 400);
}
if(showingWinScreen) {
canvasContext.font = '16px Arial';
canvasContext.fillStyle = 'white';
canvasContext.fillText("You won!", 395, 200);
canvasContext.fillText("Total Score :" + totalScore, 380, 300);
canvasContext.fillText("click to advance to next level", 380, 400);
}
//Score
canvasContext.font = '16px Arial';
canvasContext.fillStyle = 'white';
canvasContext.fillText("Score: "+playerScore, 8, 20);
//Lives
canvasContext.font = "16px Arial";
canvasContext.fillStyle = "white";
canvasContext.fillText("Lives: "+playerLives, canvas.width-65, 20);
collisionDetection();
drawBricks();
//draws the paddle
colorRect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight, 'white');
//draws the ball
colorCircle(ballX, ballY, 10, 'white');
}
以下是其中调用的函数:
function drawBricks() {
for (c=0; c < brickColumnCount; c++) {
colors = ['yellow', 'red', 'purple', 'maroon', 'green', 'gray', 'blue']
color = colors[Math.floor(Math.random()*colors.length)];
for(r=0; r < brickRowCount; r++) {
if(bricks[c][r].status == 1) {
var brickX = (c*(brickWidth+brickPadding))+brickOffsetLeft;
var brickY = (r*(brickHeight+brickPadding))+brickOffsetTop;
bricks[c][r].x = 0;
bricks[c][r].y = 0;
colorRect(brickX, brickY, brickWidth, brickHeight, color);
}
}
}
}
function colorCircle(centerX, centerY, radius, drawColor) {
canvasContext.fillStyle = drawColor;
canvasContext.beginPath();
canvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true);
canvasContext.fill();
}
function colorRect(leftX, topY, width, height, drawColor) {
canvasContext.fillStyle = drawColor;
canvasContext.fillRect(leftX, topY, width, height);
}
感谢您抽出时间阅读我的代码,并提供答案。
路易斯
答案 0 :(得分:0)
问题在于此块
//Local functions//
//function handleMouseClick(evt) {
if(showingLoseScreen) {
gameReset();
} else if(showingWinScreen) {
playerScore = 0;
//level++
showingWinScreen = false;
} else if(showingFinalScreen) {
gameReset();
//}
在最后一个else if
,你有一个缺少的近距离括号。因此,无论是否要取消注释该功能,都应该关闭该块。
//Local functions//
//function handleMouseClick(evt) {
if(showingLoseScreen) {
gameReset();
} else if(showingWinScreen) {
playerScore = 0;
//level++
showingWinScreen = false;
} else if(showingFinalScreen) {
gameReset();
}
//}