Using array of images to spawn enemies in game

Thanks for the guidance, I’ve looked through the resources provided and others that I found online and wrote some code for what I’m trying to achieve.

I’m dealing with a problem:

When the first image or “enemy” spawns, it will work correctly for the length of spawnInterval, but after this period, instead of a new image spawning at x = 0, the current image on the sketch will teleport to a new randomised y co-ordinate, with the speed noticeably increasing once this happens.

As well as that, a new image spawns on top of the previous one and they move simultaneously.

My goal is to have it work so a new image will spawn at every 2.5 second interval, and that the images will disappear once they reach the width of the sketch, which in this case is 800px. I also want the speed to stay the same for every enemy.

I’m unsure how to achieve that at this point, any help would be appreciated.

Here is my starting code:

String[] imageNames = {"square1.png", "square2.png", "square3.png", "square4.png", "square5.png"};
PImage[] images = new PImage[imageNames.length];
PImage bg;
int playerX, playerY, currentTime;
int spawnInterval = 2500;
float y, startTime;
float x = 0;
float speed = 3;
boolean[] imageVisible = new boolean[images.length];

void setup()
{
  bg = loadImage("background.png");
  size(800, 600);
  playerX = width/2;
  playerY = height/2;
  noCursor();
  startTime = millis();

  for (int i = 0; i < images.length; i++) {
    images[i] = loadImage(imageNames[i]);
    imageVisible[i] = false;
  }
}

void draw()
{
  image(bg, 0, 0);
  timeLoop();
  imageMovement();
  playerX = mouseX;
  playerY = mouseY;
  fill(255, 0, 0);
  ellipse(playerX, playerY, 30, 30);
}

void imageMovement()
{
  for (int i = 0; i < images.length; i++)
  {
    if (imageVisible[i])
    {
      x+= speed;
      image(images[i], x, y);
      if (x >= width)
      {
        imageVisible[i] = false;
      }
    }
  }
}

void timeLoop()
{
  currentTime = millis();
  if (currentTime - startTime >= spawnInterval)
  {
    spawnImage();
    startTime = currentTime;
  }
}

void spawnImage()
{
  int randomIndex = int(random(images.length));
  y = random(height - images[randomIndex].height);
  image(images[randomIndex], x, y);
  imageVisible[randomIndex] = true;
}