'Javascript Error on p5.js (Unexpected end of input)
I have a problem in p5.js. I was creating this for a school project, and everything works (I think). However, when I run it, it shows me "Unexpected end of input." And the error says "Unrecoverable syntax error (100% scanned). I'm not sure what's wrong.
I included the code below in case if there's something wrong with my code. Thanks for your time!
/* GLOBAL VARIABLES */
let meteorX = []; // Store X position of the meteor
let meteorY = [0, 0 ,0 ,0 ,0]; // Store Y position of the meteor
let meteorDiameter = []; // Store diameter of the meteor
let catcherWidth = 40; // Store diameter of the catcher
let catcherHeight = 60
let distance; //distance between catcher and meteor
let speed = []; //Speed of meteor
let score = 0; //number of meteor caught
//Declare a variables to store the image files
let bgImage;
let meteorImage;
let catcherImage;
// Only runs once
function setup() {
createCanvas(400, 400);
//Load the images into the sketch and store in variables
bgImage = loadImage('bgImage.jpg');
meteorImage = loadImage('meteor.png');
catcherImage = loadImage('rocketship.png');
}
//populate meteorX values
for(let i = 0; i < 5; i++){
meteorX[i] = random(0, width);
}
//populate initial diameter
for(let i = 0; i < 5; i++){
meteorDiameter[i] = random(10,40)
}
//populate initial speed
for(let i = 0; i < 5; i++){
speed[i] = random(0.5,4)
}
// Runs over and over in a loop
function draw() {
// Draws the image starting from the top left corner
imageMode(CORNER);
background(bgImage);
noStroke();
//Draw meteors to screen
for(let i = 0; i < 5; i++){
ellipse(meteorX[i], meteorY[i], meteorDiameter[i],meteorDiameter[i])
}
for(let i = 0; i <5; i++){
meteorY[i] = meteorY[i] + speed[i]
}
// Draw the catcher to follow the mouse
image(catcherImage, mouseX, mouseY, catcherWidth, catcherHeight);
for(let i = 0; i < 5; i++){
distance[i] = dist(meteorX[i], meteorY[i], mouseX, mouseY);
//whenever a meteor intersects with catcher
for(let i = 0; i < 5; i++){
if(distance[i] < 15){
meteorY[i] = 0
meteorX[i] = random(0,width)
meteorDiameter = random(10,40)
score = score + 1
}
}
//when meteor touches bottom of screen
for(let i = 0; i < 5; i++){
if(distance[i] > height){
meteorY[i] = 0;
meteorX[i] = random(0, width);
meteorDiameter[i] = random(10,40)
}
}
// Test to see if cat has intersected with screen bottom
if(meteorY > height) {
meteorY = 0;
meteorX = random(width);
speed = random(1,4);
meteorDiameter = random(10,30);
}
// put score on bottom
fill(0, 254, 202);
textAlign(RIGHT);
textSize(15);
text('Score: ' + score, 80, 385);
if(score == 10){
meteorY = width + 10;
meteorX = height + 10;
speed = 0;
//win message
textAlign(CENTER);
textSize(20);
text('You Win!', width/2, height/2);
textSize(14);
text('Click the mouse anywhere to restart.', width/2, height/2 + 30);
// Restart the game if player clicks the mouse.
if(mouseIsPressed){
restart();
}
}
}
function restart(){
meteorY = 0
meteorX = 100
speed = 0.5
score = 0
}```
Solution 1:[1]
Just close the draw function with }, you opened but not closed.
function draw() {
// Draws the image starting from the top left corner
imageMode(CORNER);
background(bgImage);
noStroke();
//Draw meteors to screen
for(let i = 0; i < 5; i++){
ellipse(meteorX[i], meteorY[i], meteorDiameter[i],meteorDiameter[i])
}
for(let i = 0; i <5; i++){
meteorY[i] = meteorY[i] + speed[i]
}
// Draw the catcher to follow the mouse
image(catcherImage, mouseX, mouseY, catcherWidth, catcherHeight);
for(let i = 0; i < 5; i++){
distance[i] = dist(meteorX[i], meteorY[i], mouseX, mouseY);
//whenever a meteor intersects with catcher
for(let i = 0; i < 5; i++){
if(distance[i] < 15){
meteorY[i] = 0
meteorX[i] = random(0,width)
meteorDiameter = random(10,40)
score = score + 1
}
}
//when meteor touches bottom of screen
for(let i = 0; i < 5; i++){
if(distance[i] > height){
meteorY[i] = 0;
meteorX[i] = random(0, width);
meteorDiameter[i] = random(10,40)
}
}
// Test to see if cat has intersected with screen bottom
if(meteorY > height) {
meteorY = 0;
meteorX = random(width);
speed = random(1,4);
meteorDiameter = random(10,30);
}
// put score on bottom
fill(0, 254, 202);
textAlign(RIGHT);
textSize(15);
text('Score: ' + score, 80, 385);
if(score == 10){
meteorY = width + 10;
meteorX = height + 10;
speed = 0;
//win message
textAlign(CENTER);
textSize(20);
text('You Win!', width/2, height/2);
textSize(14);
text('Click the mouse anywhere to restart.', width/2, height/2 + 30);
// Restart the game if player clicks the mouse.
if(mouseIsPressed){
restart();
}
}
}
} <--- This one is missing
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Haussi |
