Я делаю версию Galaga в JS для проекта Uni.Я объявил «bullets» как группу спрайтов с именем «bullet», но я получаю эту ошибку при попытке перебрать «bullets.length».
// Строка 132 является 4-ым циклом внутри ufo1'function.
sketch.js: 132 Uncaught TypeError: Невозможно прочитать свойство' length 'из неопределенного
в ufo1 (sketch.js: 132) при рисовании (sketch.js: 70) вp5.redraw (p5.js: 61731) на p5.(p5.js: 55256) в _runIfPreloadsAreDone (p5.js: 55174) в p5._decrementPreload (p5.js: 55182) в Image.img.onload (p5.js: 66637)
Вот мой коддля справки:
//enemy1
let enemies1;
let enemy1x = 400;
let enemy1y = 100;
let enemy1speed = 3;
let previousY = 0;
let dropY = 40;
//stars
let starNum = 300;
let starSize = 5;
let starX = new Array(starNum);
let starY = new Array(starNum);
let starDirX;
let starDirY;
let starCol1 = new Array(starNum);
let starCol2 = new Array(starNum);
//bullets
let bullets;
let bulletSpeed = -20;
//player
let ships = new Array();
let shipSpeed = 20;
function preload(){
spaceship01 = loadImage('images/spaceship01.png');
spaceship02 = loadImage('images/spaceship02.png');
spaceship03 = loadImage('images/spaceship03.png');
spaceship04 = loadImage('images/spaceship04.png');
bullet00 = loadImage('images/bullet01.png');
}
function setup() {
createCanvas(800, 800);
//star loop
for (let i = 0; i < starNum; i++) {
starX[i] = random(width);
starY[i] = random(height);
starDirX = 2;
starDirY = 3;
starCol1[i] = random(227, 235);
starCol2[i] = random(209, 237);
}
//player ship
shipX = width/2;
shipY = height-100;
ship = createSprite(shipX, shipY);
ship.addAnimation('flying', 'images/spaceship01.png', 'images/spaceship03.png');
ships.push(ship);
ship.scale = 1;
//enemies1
enemies1 = new Group();
for (i = 0; i < 5; i++) {
let enemy1 = createSprite(enemy1x + i * 40, enemy1y, 30, 30);
enemy1.addAnimation('invading1', 'images/ufo00.png', 'images/ufo02.png');
enemies1.add(enemy1);
}
}
function draw() {
background(25);
stars();
drawSprites();
move();
ufo1();
}
function stars() {
for (let i = 0; i < starX.length; i++) {
fill(starCol1[i], starCol2[i], 0);
square(starX[i], starY[i], starSize, starSize);
starX[i] += starDirX;
starY[i] += starDirY;
if (starX[i] > width) {
starX[i] -= width;
}
if (starY[i] > height) {
starY[i] -= height;
}
}
}
function move() {
if (keyIsDown(LEFT_ARROW) && keyIsDown(RIGHT_ARROW)) {
ship.setVelocity(0, 0);
} else if (keyIsDown(LEFT_ARROW)) {
ship.setVelocity(-shipSpeed, 0);
} else if (keyIsDown(RIGHT_ARROW)) {
ship.setVelocity(shipSpeed, 0);
} else {
ship.setVelocity(0, 0);
}
if (ship.position.x - ship.width/2 < 0) {
ship.position.x = 0 + ship.width/2;
}
if (ship.position.x + ship.width/2 > width) {
(ship.position.x = width - ship.width/2);
}
}
function keyPressed() {
bullets = new Group();
if (keyCode === UP_ARROW) {
bullet = createSprite(ship.position.x, ship.position.y - 120);
bullet.addAnimation('firing', 'images/bullet01.png');
bullet.setVelocity(0, bulletSpeed);
bullets.add(bullet);
}
}
function ufo1() {
for (i = 0; i < enemies1.length; i++) { enemies1[i].setVelocity(enemy1speed, 0);
}
for (i = 0; i < enemies1.length; i++) {
if (enemies1[i].position.x > width - enemies1[i].width/2) {
drop();
}
}
for (i = 0; i < enemies1.length; i ++) {
if (enemies1[i].position.x < enemies1[i].width/2) {
drop();
}
}
for (i = 0; i < bullets.length; i++) {
enemies1.overlap(bullets[i], explode);
}
}
function drop() {
for (i = 0; i < enemies1.length; i ++) {
previousY = enemies1[i].position.y;
previousY += dropY;
enemies1[i].setVelocity(0, 2.5);
if (enemies1[i].position.y = previousY + dropY) {
enemy1speed = -enemy1speed;
enemies1[i].setVelocity(enemy1speed, 0);
}
}
}
function explode() {
enemy1.remove();
}
Любая помощь с благодарностью!Приветствия