Javascript P5.js游戏因数组中缺少元素的TypeError而崩溃
好的,所以我遇到了这样一个问题,我的程序经常在游戏中途崩溃,原因是TypeError:uncaughttypeerror:子弹[I]未定义 该错误发生在函数中,我检查子弹和敌人数组中的每个变量是否存在冲突。 这是我创建的这个小型学校项目的源代码,我一直在尝试解决这个特定的bug,现在已经有好几个小时了。如果有人能够帮助我,那将是令人惊讶的,因为我不能完全理解问题可能是什么,除了当一个元素已经拼接时发生碰撞。但是,由于P5.js中的draw()函数每秒运行60次,这种情况不应该像它那样经常发生,所以我不明白这怎么会成为一个问题Javascript P5.js游戏因数组中缺少元素的TypeError而崩溃,javascript,crash,typeerror,p5.js,Javascript,Crash,Typeerror,P5.js,好的,所以我遇到了这样一个问题,我的程序经常在游戏中途崩溃,原因是TypeError:uncaughttypeerror:子弹[I]未定义 该错误发生在函数中,我检查子弹和敌人数组中的每个变量是否存在冲突。 这是我创建的这个小型学校项目的源代码,我一直在尝试解决这个特定的bug,现在已经有好几个小时了。如果有人能够帮助我,那将是令人惊讶的,因为我不能完全理解问题可能是什么,除了当一个元素已经拼接时发生碰撞。但是,由于P5.js中的draw()函数每秒运行60次,这种情况不应该像它那样经常发生,所
function setup() {
createCanvas(displayWidth - 20, displayHeight - 20);
angleMode(DEGREES);
rectMode(CENTER);
ellipseMode(CENTER);
playerX = width / 2;
playerY = height / 2;
}
let playerX = 0;
let playerY = 0;
let angle = 0;
let timer = 120;
let time = 0;
let v = 2;
let cooldown = 20;
let color = 50;
let hit = 0;
var bullets = [];
var enemies_easy = [];
var enemies_hard = [];
var score = 0;
var highscore = 0;
var gameover = false;
var gamestarted = false;
var nexthard = 5;
var currentenemy = 0;
function draw() {
if (!gamestarted) {
push();
textAlign(CENTER);
background('black');
translate(displayWidth / 2, displayHeight / 2);
textSize(width / 50);
fill('white');
text('Welcome!', 0, -100);
text('Controls:', 0, 0);
text('Fullscreen: F Moving: W, A, S, D Restart: R Shooting: Left Mouse Button Start: Space Bar', 0, 100);
pop();
if (keyIsDown(32)) {
bullets = [];
gamestarted = true;
}
} else {
if (!gameover) {
//calculates shot enemies_easy
bulletcollision();
//shoots if weapon cooldown is expired
background('black');
cooldown--;
activategun();
//draw hero
angle = atan2(mouseY - playerY, mouseX - playerX) + 90;
push();
fill('blue');
translate(playerX, playerY);
rotate(angle);
ellipse(0, 0, 40, 40);
//draw gun
fill('grey');
rect(0, 0 - 25, 10, 20);
pop();
//move hero
move();
//creates enemies with increasing difficulty
time++;
difficulty();
spawnenemy_easy();
spawnenemy_hard();
//shows score on screen
showscore();
//draw crosshair
noCursor();
push();
fill('white');
stroke('white');
line(mouseX, mouseY, mouseX + 20, mouseY);
line(mouseX, mouseY, mouseX - 20, mouseY);
line(mouseX, mouseY, mouseX, mouseY + 20);
line(mouseX, mouseY, mouseX, mouseY - 20);
pop();
//checks for game over
playercollision();
} else {
if (keyIsDown(82)) {
bullets = [];
enemies_easy = [];
enemies_hard = [];
timer = 120;
time = 0;
cooldown = 20;
score = 0;
playerX = width / 2;
playerY = height / 2;
v = 2;
gameover = false;
}
}
}
}
class bullet {
constructor() {
this.x = playerX;
this.y = playerY;
this.angle = createVector(mouseX - playerX, mouseY - playerY);
this.angle.normalize();
}
drawbullet() {
push();
fill('white');
ellipse(this.x, this.y, 10, 10);
pop();
this.y = this.y + 10 * this.angle.y;
this.x = this.x + 10 * this.angle.x;
}
}
class enemy_easy {
constructor() {
this.x = random(-1000, width + 1000);
if (this.x > width || this.x < 0) {
if (this.x > width) {
this.x = width;
this.y = random(0, height + 1);
}
if (this.x < 0) {
this.x = 0;
this.y = random(0, height + 1);
} else {}
} else {
let i = floor(random(0, 2));
this.y = i * height;
}
}
drawenemy_easy() {
push();
this.angle = createVector(this.x - playerX, this.y - playerY);
this.angle.normalize();
fill('red');
ellipse(this.x, this.y, 30, 30);
rotate(angle);
pop();
this.x = this.x - v * this.angle.x; // * random(0, 5);
this.y = this.y - v * this.angle.y; // * random(0, 5);
}
}
class enemy_hard {
constructor() {
this.x = random(-1000, width + 1000);
if (this.x > width || this.x < 0) {
if (this.x > width) {
this.x = width;
this.y = random(0, height + 1);
}
if (this.x < 0) {
this.x = 0;
this.y = random(0, height + 1);
} else {}
} else {
let i = floor(random(0, 2));
this.y = i * height;
}
}
drawenemy_hard() {
push();
this.angle = createVector(this.x - playerX, this.y - playerY);
this.angle.normalize();
fill('purple');
ellipse(this.x, this.y, 30, 30);
rotate(angle);
pop();
this.x = this.x - v * this.angle.x; // * random(0, 5);
this.y = this.y - v * this.angle.y; // * random(0, 5);
}
}
function keyPressed() {
//fullscreen Taste F: https://www.geeksforgeeks.org/p5-js-fullscreen-function/
if (keyCode === 70) {
let fs = fullscreen();
fullscreen(!fs);
}
}
function move() {
if (keyIsDown(83) && playerY <= height - 22) {
playerY = playerY + 4;
}
if (keyIsDown(87) && playerY >= 22) {
playerY = playerY - 4;
}
if (keyIsDown(68) && playerX <= width - 22) {
playerX = playerX + 4;
}
if (keyIsDown(65) && playerX >= 22) {
playerX = playerX - 4;
}
}
function activategun() {
for (var i = 0; i < bullets.length; i++) {
bullets[i].drawbullet();
if (bullets[i].x <= 0 || bullets[i].y <= 0 || bullets[i].x >= width || bullets[i].y >= height) {
bullets.splice(i, 1);
}
}
}
function mousePressed() {
if (cooldown < 0) {
bullets.push(new bullet());
cooldown = 20;
}
}
function spawnenemy_easy() {
for (var i = 0; i < enemies_easy.length; i++) {
enemies_easy[i].drawenemy_easy();
}
}
function spawnenemy_hard() {
for (var i = 0; i < enemies_hard.length; i++) {
enemies_hard[i].drawenemy_hard();
}
}
function newenemy() {
if (currentenemy < nexthard) {
enemies_easy.push(new enemy_easy());
currentenemy++;
} else {
enemies_hard.push(new enemy_hard());
currentenemy = 0;
nexthard = random(2, 6);
}
}
function bulletcollision() {
for (var i = 0; i < bullets.length; i++) {
for (var e = 0; e < enemies_easy.length; e++) {
var distance = createVector(bullets[i].x - enemies_easy[e].x, bullets[i].y - enemies_easy[e].y);
if (distance.mag() <= 18) {
bullets.splice(i, 1);
enemies_easy.splice(e, 1);
score++;
if (score > highscore) {
highscore++;
}
}
}
for (var e = 0; e < enemies_hard.length; e++) {
var distance = createVector(bullets[i].x - enemies_hard[e].x, bullets[i].y - enemies_hard[e].y);
if (distance.mag() <= 18) {
bullets.splice(i, 1);
hit++;
if (hit == 2) {
enemies_hard.splice(e, 1);
score++;
if (score > highscore) {
highscore++;
}
hit = 0;
}
}
}
}
}
function playercollision() {
for (var i = 0; i < enemies_easy.length; i++) {
var distance = createVector(enemies_easy[i].x - playerX, enemies_easy[i].y - playerY);
if (distance.mag() <= 25) {
push();
background(abs(255 * sin(random(0, 255))), 255 * sin(random(0, 255)), 255 * sin(random(0, 255)));
fill('white');
textAlign(CENTER);
textSize(width / 40);
translate(width / 2, height / 2);
text("Game Over!", 0, -100);
text("Score: " + score, 0, 0);
text("High Score: " + highscore, 0, 100);
pop();
return gameover = true;
}
}
for (var i = 0; i < enemies_hard.length; i++) {
var distance = createVector(enemies_hard[i].x - playerX, enemies_hard[i].y - playerY);
if (distance.mag() <= 25) {
push();
background(abs(255 * sin(random(0, 255))), 255 * sin(random(0, 255)), 255 * sin(random(0, 255)));
fill('white');
textAlign(CENTER);
textSize(width / 40);
translate(width / 2, height / 2);
text("Game Over!", 0, -100);
text("Score: " + score, 0, 0);
text("High Score: " + highscore, 0, 100);
pop();
return gameover = true;
}
}
}
function difficulty() {
if (time >= timer) {
newenemy();
time = 0;
timer = timer * 0.99;
v = v * 1.02;
}
}
function showscore() {
push();
fill('white');
textSize(width / 55);
textAlign(CENTER);
text('Score: ' + score, 100, 100);
pop();
}
函数设置(){
createCanvas(displayWidth-20,displayHeight-20);
角度模式(度);
矩形模式(中心);
ellipseMode(中心);
playerX=宽度/2;
playerY=身高/2;
}
设playerX=0;
设playerY=0;
设角度=0;
设定时器=120;
设时间=0;
设v=2;
让冷却时间=20;
颜色=50;
让hit=0;
var=[];
var_easy=[];
var_hard=[];
var得分=0;
var高分=0;
var gameover=false;
var=false;
var nexthard=5;
var=0;
函数绘图(){
如果(!gamestarted){
推();
文本对齐(中心);
背景(“黑色”);
平移(显示宽度/2,显示高度/2);
文本大小(宽度/50);
填充(“白色”);
文本('Welcome!',0,-100);
文本('控件:',0,0);
文本('全屏:F移动:W,A,S,D重新启动:R拍摄:鼠标左键开始:空格键',0,100);
pop();
如果(keyIsDown(32)){
子弹=[];
gamestarted=true;
}
}否则{
如果(!gameover){
//计算射击敌人很容易
bulletcollision();
//武器冷却时间到期时射击
背景(“黑色”);
冷却--;
activategun();
//画英雄
角度=atan2(鼠标-游戏机,鼠标-游戏机x)+90;
推();
填充(“蓝色”);
翻译(playerX,playerY);
旋转(角度);
椭圆(0,0,40,40);
//拉枪
填充(“灰色”);
rect(0,0-25,10,20);
pop();
//移动英雄
move();
//制造敌人的难度越来越大
时间++;
难度();
(容易的);
生下你的敌人;
//在屏幕上显示分数
showscore();
//画十字线
noCursor();
推();
填充(“白色”);
笔划(“白色”);
行(mouseX,mouseY,mouseX+20,mouseY);
行(mouseX,mouseY,mouseX-20,mouseY);
行(mouseX,mouseY,mouseX,mouseY+20);
行(mouseX,mouseY,mouseX,mouseY-20);
pop();
//检查游戏是否结束
playercollision();
}否则{
如果(keyIsDown(82)){
子弹=[];
敌人轻松=[];
敌人硬=[];
定时器=120;
时间=0;
冷却时间=20;
得分=0;
playerX=宽度/2;
playerY=身高/2;
v=2;
gameover=false;
}
}
}
}
等级子弹{
构造函数(){
this.x=playerX;
这个.y=游戏性;
this.angle=createVector(mouseX-playerX,mouseY-playerY);
这个.angle.normalize();
}
drawbullet(){
推();
填充(“白色”);
椭圆(这个.x,这个.y,10,10);
pop();
this.y=this.y+10*this.angle.y;
这个.x=这个.x+10*这个角度.x;
}
}
班上的敌人很容易{
构造函数(){
x=随机(-1000,宽度+1000);
if(this.x>宽度| | this.x<0){
如果(此.x>宽度){
x=宽度;
y=随机(0,高度+1);
}
如果(此.x<0){
这个.x=0;
y=随机(0,高度+1);
}else{}
}否则{
设i=楼层(随机(0,2));
y=i*高度;
}
}
付款人{
推();
this.angle=createVector(this.x-playerX,this.y-playerY);
这个.angle.normalize();
填充(“红色”);
椭圆(这个.x,这个.y,30,30);
旋转(角度);
pop();
this.x=this.x-v*this.angle.x;//*随机(0,5);
this.y=this.y-v*this.angle.y;//*随机(0,5);
}
}
阶级仇敌{
构造函数(){
x=随机(-1000,宽度+1000);
if(this.x>宽度| | this.x<0){
如果(此.x>宽度){
x=宽度;
y=随机(0,高度+1);
}
如果(此.x<0){
这个.x=0;
y=随机(0,高度+1);
}else{}
}否则{
设i=楼层(随机(0,2));
y=i*高度;
}
}
付款人{
推();
this.angle=createVector(this.x-playerX,this.y-playerY);
这个.angle.normalize();
填充(“紫色”);
椭圆(这个.x,这个.y,30,30);
旋转(角度);
pop();
this.x=this.x-v*this.angle.x;//*随机(0,5);
this.y=this.y-v*this.angle.y;//*随机(0,5);
}
}
功能键按下(){
//全屏口味F:https://www.geeksforgeeks.org/p5-js-fullscreen-function/
如果(键代码===70){
设fs=fullscreen();
全屏(!fs);
}
}
函数move(){
if(keyIsDo)