In my main class i have made an array to spawn a few green circles. I tried to get the background to change when i clicked on one of them with a boolean which is supposed to come true when clicking while over the circle... I am new to programming as a whole and I am not really sure how to solve this problem.. I dont really know where the data of the x and y positions of the green circles are displayed and how the klicked function is supposed to find them because all of them are created at random positions and in an other class. So here is my code so far:
class Gesicht {
//Eigenschaften
boolean fail = false;
float whEllipse= 200;
float yPos = random(-whEllipse, height);
float xPos = random(-whEllipse, height);
float xSpeed = 1;
float P = 1;
//Constructor
Gesicht() {
// this.yPos = yPos;
// this.xPos = xPos;
}
//Methoden/Funktionen
void move() {
//paint();
xPos += xSpeed;
yPos += (P*sin(radians(xPos))+1); //+1 macht dass sich das Gesicht langsam runter bewegt.
if (xPos>= width+whEllipse/2) {
// P = P+1; Maybe falls ich größere Amplitude will
xPos = -whEllipse/2;
}
if (yPos >= height+whEllipse/2) {
yPos = random(-whEllipse, height/2);
xPos = random(-200, whEllipse/2);
}
}
boolean klicked() {
if (mouseX > xPos-whEllipse/2 && mouseX < xPos+whEllipse/2
&& mouseY > yPos-whEllipse/2 && mouseY < yPos+whEllipse/2
&& mousePressed) {
return true;
}
return false;
}
void klick() {
if (klicked()) {
fail = true;
}
}
void spawn() {
move();
fill(0, 255, 0);
circle(xPos, yPos, whEllipse);
}
}
this is the class where i tried to put my funktions.
int farbe = 255;
Gesicht[] grün;
Gesichter[] rot;
Gesichterer[] blau;
void settings() {
size(700, 700);
}
void setup() {
rot = new Gesichter[10];
for ( int i = 0; i< rot.length; i++) {
rot[i]= new Gesichter();
}
grün = new Gesicht[15];
for ( int i = 0; i< grün.length; i++) {
grün[i]= new Gesicht();
}
blau = new Gesichterer[20];
for ( int i = 0; i< blau.length; i++) {
blau[i]= new Gesichterer();
}
}
void draw() {
background(farbe);
if(variable.klicked()){
background(150,0,0);
}
for ( int i = 0; i< grün.length; i++) {
grün[i].spawn();
}
for ( int i = 0; i< rot.length; i++) {
rot[i].spawn();
}
for (int i = 0; i< blau.length; i++) {
blau[i].spawn();
}
}
And this is my main class.
The funktion never gets true in the moment... I hope someone can help me :// Would be very grateful.
You might be trying a bit hard without cleaning up your code which on long run leads to more headaches.
I say based on the following:
klicked()
but also a klick()
method which doesn't do much: klick()
should sufficeGesicht
class, however you additioanlly attempt to instantiate Gesichter
and Gesichterer
which don't exist (at least not in the code you posted)if(variable.klicked()){ background(150,0,0); }
refers to an unusuable variable called variable
(a placeholder perhaps?). Even if that variable existed, the red background would've only briefly flashed while klicked()
was true.You're very close though:
klicked()
method does what you wantfarbe
already stores the background colourYou could do something like this:
class Gesicht {
//Eigenschaften
boolean fail = false;
float whEllipse= 200;
float yPos = random(-whEllipse, height);
float xPos = random(-whEllipse, height);
float xSpeed = 1;
float P = 1;
//Constructor
Gesicht() {
// this.yPos = yPos;
// this.xPos = xPos;
}
//Methoden/Funktionen
void move() {
//paint();
xPos += xSpeed;
yPos += (P*sin(radians(xPos))+1); //+1 macht dass sich das Gesicht langsam runter bewegt.
if (xPos>= width+whEllipse/2) {
// P = P+1; Maybe falls ich größere Amplitude will
xPos = -whEllipse/2;
}
if (yPos >= height+whEllipse/2) {
yPos = random(-whEllipse, height/2);
xPos = random(-200, whEllipse/2);
}
}
boolean klicked() {
if (mouseX > xPos-whEllipse/2 && mouseX < xPos+whEllipse/2
&& mouseY > yPos-whEllipse/2 && mouseY < yPos+whEllipse/2
&& mousePressed) {
return true;
}
return false;
}
void klick() {
if (klicked()) {
fail = true;
}
}
void spawn() {
move();
fill(0, 255, 0);
circle(xPos, yPos, whEllipse);
}
}
int farbe = 255;
Gesicht[] green;
Gesicht[] rot;
Gesicht[] blau;
void settings() {
size(700, 700);
}
void setup() {
//rot = new Gesicht[10];
//for ( int i = 0; i< rot.length; i++) {
// rot[i]= new Gesicht();
//}
//green = new Gesicht[15];
//for ( int i = 0; i< green.length; i++) {
// green[i]= new Gesicht();
//}
blau = new Gesicht[20];
for ( int i = 0; i< blau.length; i++) {
blau[i]= new Gesicht();
}
}
void draw() {
background(farbe);
//if (variable.klicked()) {
// background(150, 0, 0);
//}
//for ( int i = 0; i< green.length; i++) {
// if(green[i].klicked()){
// farbe = color(red(farbe), random(255), blue(farbe));
// }
// green[i].spawn();
//}
//for ( int i = 0; i< rot.length; i++) {
// rot[i].spawn();
//}
for (int i = 0; i< blau.length; i++) {
if(blau[i].klicked()){
farbe = color(red(farbe), green(farbe), random(255));
}
blau[i].spawn();
}
}
(Only one array of circles is uncommented for simplicity/easy testing. Feel free to adjust as needed)
One final touch you could add, if you want the background to only change once when you click (as opposed to continously while the mouse is pressed), is debouncing. The idea is to use a state/variable to keep track if the button was previously pressed or not to only chance the background once until the states change again. (You could implement this using an extra boolean variable in your class or perhaps simpler, use the mouseClicked() callback to iterate over your array simply to check which instance has been klicked()
to change the background accordingly).