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

**URL:** <https://discourse.processing.org/t/how-can-i-create-objects-to-make-this-work-spaceships/32017>\
**Category:** Coding Questions\
**Created:** [August 30, 2021, 10:33pm UTC](https://discourse.processing.org/t/how-can-i-create-objects-to-make-this-work-spaceships/32017 "2021-08-30T22:33:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucaswoah](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@lucaswoah](https://discourse.processing.org/u/lucaswoah)\
**Post date:** [August 30, 2021, 10:33pm UTC](https://discourse.processing.org/t/how-can-i-create-objects-to-make-this-work-spaceships/32017/1 "2021-08-30T22:33:59Z")

</div>

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];  
}  
}

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [September 2, 2021, 9:23am UTC](https://discourse.processing.org/t/how-can-i-create-objects-to-make-this-work-spaceships/32017/2 "2021-09-02T09:23:39Z")

</div>

Hi @lucaswoah,

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

> [@lucaswoah](#):
>
> 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!

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!

> [@Problems with my mini game](https://discourse.processing.org/t/problems-with-my-mini-game/21949/2):
>
> Hi, Welcome to the community! slight_smile Before trying to solve your problem, you are facing a well known problem where the structure of your code is actually describing [object oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming) : So object oriented programming is a way to structure your code by using objects to model the data that your are manipulating. In this special case, your are creating Balls, and a Ball is described as : a position (x and y coordinates on the screen) a speed (described as a vector, xDir and y…

Also a recent post by @GoToLoop :

> [@Individualize functions in P5.js](https://discourse.processing.org/t/individualize-functions-in-p5-js/32051/3):
>
> Although written for Processing’s Java Mode these 2 OOP articles can be easily applied to any Processing flavor: [forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays.html](http://forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays.html)[forum.Processing.org/two/discussion/8081/from-several-arrays-to-classes.html](http://forum.Processing.org/two/discussion/8081/from-several-arrays-to-classes.html) This is how we create classes in JS btW:

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 6, 2021, 9:02pm UTC](https://discourse.processing.org/t/how-can-i-create-objects-to-make-this-work-spaceships/32017/3 "2021-09-06T21:02:33Z")

</div>

example

see [https://processing.org/tutorials/objects](https://processing.org/tutorials/objects)

```auto

// 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
//

```
