We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys, I'm working on a modification of the BouncyBubbles example. Basically I have these words bouncing around and I want to integrate a function where the word color will change to white when the mouse hovers over it. Here's my code so far, and I'm not sure what's wrong. The words do change color, but not when the mouse hovers over them. All help will be appreciated!
int numBalls = 12;
float spring = 0.03;
float gravity = 0;
float friction = -1;
boolean overSecret = false;
Ball[] balls = new Ball[numBalls];
void setup() {
size(1000, 700);
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls);
}
noStroke();
}
void draw() {
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].move();
balls[i].display();
balls[i].checkButtons();
}
}
class Ball {
float x, y;
float diameter;
float vx = random (1,3);
float vy = random (1,3);
color rcolor = color (random (255), random (255), random (255));
int id;
Ball[] others;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void move() {
vy += gravity;
x += vx;
y += vy;
if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= friction;
}
else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
if (overSecret == true) {
fill(255);
} else {
fill (rcolor);
}
textSize (30);
text ("secret", x, y);
}
void checkButtons () {
if (mouseX > x - 50 && mouseX < x + 90 && mouseY > y - 20 && mouseY < y + 20) {
overSecret = true;
} else {
overSecret = false;
}
}
}
Answers
I've got these 2 online examples:
http://studio.processingtogether.com/sp/pad/export/ro.9eDRvB4LRmLrr/latest
http://studio.processingtogether.com/sp/pad/export/ro.9V7R1wu55fOiN/latest
Put the checkButtons() before the display(). Currently the next text item is being hightlighted.