流星捕手没有运行,不知道为什么



我正在为我的学校项目创建一个流星捕手游戏,我创建了一个流星捕手游戏和一个重启功能,当你按下鼠标。但是,当您达到所需的分数并单击后,它会在控制台中显示错误。我不知道该怎么办。

下面是代码和错误信息。

谢谢你的时间!

/* GLOBAL VARIABLES */
let meteorX = []; // Create array to store X positions of meteors
let meteorY = [0, 0, 0, 0, 0]; // Create array to store Y positions of meteors
let meteorDiameter = []; // Create array to store diameter of meteor
let catcherWidth = 40; // Store diameter of the catcher
let catcherHeight = 60
let distance = []; // Create array to store the distance between meteor and catcher
let speed = []; // Create array to store meteor speed
let score = 0; // Store the number of meteors 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 initial X values
for(let i = 0; i < 5; i++){
meteorX[i] = random(0, width);
//print('meteorX = ' + meteorX[i]);
}

// Populate initial diameter values
for(let i = 0; i < 5; i++){
meteorDiameter[i] = random(10, 40);
//print('meteorDiameter = ' + meteorDiameter[i]);
}

// Populate initial speed values
for(let i = 0; i < 5; i++){
speed[i] = random(0.5, 4);
//print('speed = ' + speed[i]);
}
}
// Runs over and over in a loop
function draw() {
background(0);
noStroke();

// Draws the image starting from the top left corner
imageMode(CORNER);
background(bgImage); 
noStroke(); 



// Draw meteors to the screen
for(let i = 0; i < 5; i++){
imageMode(CENTER)
image(meteorImage, meteorX[i], meteorY[i], meteorDiameter[i], meteorDiameter[i])

}

// Cycle through meteors to make them fall at different speeds
for(let i = 0; i < 5; i++){
meteorY[i] = meteorY[i] + speed[i];
}

// Draw the catcher to follow the mouse
imageMode(CENTER)
image(catcherImage, mouseX, mouseY, catcherWidth, catcherHeight);

// Cycle through meteors to determine distance between meteor and catcher
for(let i = 0; i < 5; i++){
distance[i] = dist(meteorX[i], meteorY[i], mouseX, mouseY);
//print('distance = ' + distance[i]);
}

// Cycle through meteors to test if meteor and catcher have intersected
for(let i = 0; i < 5; i++){
if(distance[i] < 25){
// Redraw  meteor to top of screen at a random location on x-axis
meteorY[i] = 0;
meteorX[i] = random(0, width);

// Set new meteor speed to random number between 1 and 4
meteorDiameter[i] = random(10, 40);

// Increase the score value by 1
score = score + 1;
}
}

// Cycle through meteors to test if intersected with screen bottom
for(let i = 0; i < 5; i++){
if(meteorY[i] > height){
meteorY[i] = 0;
meteorX[i] = random(0, width);
meteorDiameter[i] = random(10, 40);
}
}

// Draw the score to the screen
fill(0, 254, 202);
textAlign(LEFT);
textSize(15);
text('Meteors Caught: ' + score, 15, 380);

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, 0 ,0 ,0 ,0];
meteorX = []
speed = [];
score = 0;
}
🌸 p5.js says: image() was expecting Number for the second parameter, received an empty variable instead. (on line 68 in sketch.js [/sketch.js:68:5])
If not intentional, this is often a problem with scope: [https://p5js.org/examples/data-variable-scope.html]. (http://p5js.org/reference/#/p5/image) 
🌸 p5.js says: image() was expecting Number for the third parameter, received an empty variable instead. (on line 68 in sketch.js [/sketch.js:68:5])
If not intentional, this is often a problem with scope: [https://p5js.org/examples/data-variable-scope.html]. (http://p5js.org/reference/#/p5/image) 
🌸 p5.js says: dist() was expecting Number for the first parameter, received an empty variable instead. (on line 84 in sketch.js [/sketch.js:84:19])
If not intentional, this is often a problem with scope: [https://p5js.org/examples/data-variable-scope.html]. (http://p5js.org/reference/#/p5/dist) 
🌸 p5.js says: dist() was expecting Number for the second parameter, received an empty variable instead. (on line 84 in sketch.js [/sketch.js:84:19])
If not intentional, this is often a problem with scope: [https://p5js.org/examples/data-variable-scope.html]. (http://p5js.org/reference/#/p5/dist) 
🌸 p5.js says: image() was expecting Number for the second parameter, received an empty variable instead. (on line 68 in sketch.js [/sketch.js:68:5])
If not intentional, this is often a problem with scope: [https://p5js.org/examples/data-variable-scope.html]. (http://p5js.org/reference/#/p5/image) 
🌸 p5.js says: dist() was expecting Number for the first parameter, received an empty variable instead. (on line 84 in sketch.js [/sketch.js:84:19])
If not intentional, this is often a problem with scope: [https://p5js.org/examples/data-variable-scope.html]. (http://p5js.org/reference/#/p5/dist) 
🌸 p5.js says: image() was expecting Number for the second parameter, received an empty variable instead. (on line 68 in sketch.js [/sketch.js:68:5])
If not intentional, this is often a problem with scope: [https://p5js.org/examples/data-variable-scope.html]. (http://p5js.org/reference/#/p5/image) ```

你实际上并没有在重启时运行你的代码,你只是重置你的变量。在重新启动函数中添加setup()函数调用后,它应该可以工作。你收到的错误是因为你通过meteorY = width + 10;将数组更改为数字,因此meteorY[i]返回未定义。所以你可以删掉这些。

/* GLOBAL VARIABLES */
let meteorX = []; // Create array to store X positions of meteors
let meteorY = [0, 0, 0, 0, 0]; // Create array to store Y positions of meteors
let meteorDiameter = []; // Create array to store diameter of meteor
let catcherWidth = 40; // Store diameter of the catcher
let catcherHeight = 60
let distance = []; // Create array to store the distance between meteor and catcher
let speed = []; // Create array to store meteor speed
let score = 0; // Store the number of meteors 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 initial X values
for(let i = 0; i < 5; i++){
meteorX[i] = random(0, width);
//print('meteorX = ' + meteorX[i]);
}

// Populate initial diameter values
for(let i = 0; i < 5; i++){
meteorDiameter[i] = random(10, 40);
//print('meteorDiameter = ' + meteorDiameter[i]);
}

// Populate initial speed values
for(let i = 0; i < 5; i++){
speed[i] = random(0.5, 4);
//print('speed = ' + speed[i]);
}
}
// Runs over and over in a loop
function draw() {
background(0);
noStroke();

// Draws the image starting from the top left corner
imageMode(CORNER);
background(bgImage); 
noStroke(); 



// Draw meteors to the screen
for(let i = 0; i < 5; i++){
imageMode(CENTER)
image(meteorImage, meteorX[i], meteorY[i], meteorDiameter[i], meteorDiameter[i])

}

// Cycle through meteors to make them fall at different speeds
for(let i = 0; i < 5; i++){
meteorY[i] = meteorY[i] + speed[i];
}

// Draw the catcher to follow the mouse
imageMode(CENTER)
image(catcherImage, mouseX, mouseY, catcherWidth, catcherHeight);

// Cycle through meteors to determine distance between meteor and catcher
for(let i = 0; i < 5; i++){
distance[i] = dist(meteorX[i], meteorY[i], mouseX, mouseY);
//print('distance = ' + distance[i]);
}

// Cycle through meteors to test if meteor and catcher have intersected
for(let i = 0; i < 5; i++){
if(distance[i] < 25){
// Redraw  meteor to top of screen at a random location on x-axis
meteorY[i] = 0;
meteorX[i] = random(0, width);

// Set new meteor speed to random number between 1 and 4
meteorDiameter[i] = random(10, 40);

// Increase the score value by 1
score = score + 1;
}
}

// Cycle through meteors to test if intersected with screen bottom
for(let i = 0; i < 5; i++){
if(meteorY[i] > height){
meteorY[i] = 0;
meteorX[i] = random(0, width);
meteorDiameter[i] = random(10, 40);
}
}

// Draw the score to the screen
fill(0, 254, 202);
textAlign(LEFT);
textSize(15);
text('Meteors Caught: ' + score, 15, 380);

if(score == 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, 0 ,0 ,0 ,0];
meteorX = [];
speed = [];
score = 0;
setup();
}

相关内容

最新更新