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