How can I create objects to make this work? (Spaceships)

So, I’m making this minigame where you have many spaceships on screen and their lights go out when you click them. I was able to make it work when you click the third spaceship (from the top to the bottom), but when I click it, the lights from every spaceship go out. I know I have to make objects to store my spaceships, but I just can’t do it!

I tried creating a “Spaceship” class and then spaceship1, spaceship2, etc as new objects from that class, but when I wrote spaceship1.move();, for example, every spaceship moved instead of just that one. Does anyone have an idea of how to solve this? Thank you very much!

//this is the starting positions for every spaceship
int [] Xpos = {482, 126, 422, 294, 162};

//this is the velocity
int [] M = new int[5];

//boolean for when I click the third spaceship
boolean [] clicked = new boolean[5];

void setup() {
size(640, 360);

//i gave them random velocities
for (int i = 0; i < M.length; i++) {
M[i] = (int)random(-10, 10);
/when the random number was zero, i had it replaced with another random number,
cause i didn’t want them to ever have zero speed
/
if (M[i] == 0)
M[i] = (int)random(1, 10);
}

/i created an array of booleans all set to false, i created an array
because eventually i want to make it so that every spaceship has its own on/off
status
/

for (int i = 0; i < clicked.length; i++) {
clicked[i] = false;
}
}

void draw() {
background(100);
spaceShip(Xpos[0]+M[0], 159, 223);
spaceShip(Xpos[1]+M[1], 89, 93);
spaceShip(Xpos[2]+M[2], 286, 84);
spaceShip(Xpos[3]+M[3], 49, 48);
spaceShip(Xpos[4]+M[4], 220, 151);
moveSpaceship();
}

void spaceShip(int x, int y, int h) {
noStroke();
fill(50);
circle(x, y, h/3);
ellipse(x, y+h/10, h/1.3, h/3.3);
fill(255);
//the lights are only shown if the status of the boolean is true//
if (clicked[0] == false) {
ellipse(x, y+h/8, h/10, h/10);
ellipse(x-h/6, y+h/9, h/10, h/10);
ellipse(x+h/6, y+h/9, h/10, h/10);
}
x = 10;
}

void moveSpaceship() {

//position is increased (or decreased) by the speed
for (int i = 0; i < Xpos.length; i++) {
Xpos[i] = Xpos[i] + M[i];
/when their position reaches a certain limit,
they start going the opposite direction
/
if (Xpos[i] >= width || Xpos[i] <= 0)
M[i] = M[i] * -1;
}
}

/* when the mouse is on the third spaceship and clicked*/

void mousePressed() {
if (mouseX >= Xpos[0]+M[0]-74 && mouseX <= Xpos[0]+M[0]+159 && mouseY >= 85 && mouseY <= 159 + 74) {
clicked[0] = !clicked[0];
}
}

Hi @lucaswoah,

Can you please format your code by using the </> button when editing a message or use triple backticks like this: ```code``` → code?

You were on the right track! What you need is classes and objects in order for your spaceships to have their own data (position, lights, size…) and be able to do actions on them.

You can check an old post I did on the subject, it should be pretty straightforward to implement!

Also a recent post by @GoToLoop :

1 Like

example

see https://processing.org/tutorials/objects


// Very clean: ONE array of type SpaceShip. No other data. All data are in the class Spaceship.
Spaceship[] spaceships = new Spaceship [5]; 

void setup() {
  size(640, 360);

  // we use local variables (known only in setup()!) that we copy in the object (in the array) using the constructor of the class

  //this is the starting positions for every spaceship
  int [] Xpos = {482, 126, 422, 294, 162};
  // DO THE SAME FOR y-values 159, 89, 286, 49, 220 and h-values 223, 93, 84, 48, 151 !!!!!!!!!!!!!!!!!!!!!!!!!!!

  //this is the velocity
  float vm;

  // loop over the slots of the array
  //i gave them random velocities vm
  for (int i = 0; i < spaceships.length; i++) {
    vm = (int)random(-10, 10);
    //when the random number was zero, i had it replaced with another random number, 
    // cause i didn't want them to ever have zero speed
    if (vm == 0)
      vm = (int)random(1, 10);

    // make an object from the class (in the slot of the array)
    spaceships[i] = new Spaceship( Xpos[i], random(40, height-40), random( 45, 222 ), vm );
  }//for
  //
}//func 

void draw() {
  background(100);
  for (Spaceship s1 : spaceships) {
    s1.display();
    s1.moveSpaceship();
  }
}

/* when the mouse is clicked*/
void mousePressed() {
  for (Spaceship s1 : spaceships) {
    if ( s1.spaceshipmousePressed() ) { 
      return;
    }//if
  }//for
}//func

//============================================================================================

class Spaceship {
  //
  // data (very simple, no arrays!!!)
  float x, y, 
    h;
  float v; 

  boolean clicked = false;

  // constructor
  Spaceship(int x_, float y_, 
    float h_, 
    float v_) {
    x=x_;
    y=y_;
    h=h_;
    v=v_;
  } // constructor

  void display() {
    noStroke();
    fill(50);
    ellipse(x, y, h/3, h/3); //top
    ellipse(x, y+h/10, h/1.3, h/3.3);// body 

    fill(255);
    //the lights are only shown if the status of the boolean is true//
    if (clicked == false) {
      ellipse(x, y+h/8, h/10, h/10);
      ellipse(x-h/6, y+h/9, h/10, h/10);
      ellipse(x+h/6, y+h/9, h/10, h/10);
    }//if
  }//func 

  void moveSpaceship() {
    //position is increased (or decreased) by the speed
    x = x + v;
    // when their position reaches a certain limit, 
    // they start going the opposite direction
    if (x > width-h/3 || x <= h/3)
      v = v * -1;
  }//func

  boolean spaceshipmousePressed() {
    // not sure here 
    if (mouseX > x-h/1.3 &&
      mouseX < x+h/1.3 && 
      mouseY >= y+h/10 - h/3.3 &&
      mouseY <= y+h/10 + h/3.3) {
      clicked = !clicked; // OR false; 
      return true; // hit
    }//if
    return false; // miss
  }
  //
}//class
//