xxxxxxxxxx
//rectangle
float x;
float y;
float w;
float h;
//button
float buttonwidth;
float buttonheight;
//ellipse
float ex;
float ey;
float ew;
float er;
float speedx;
float speedy;
boolean clicked = false;
void setup () {
size (400, 400);
// button
// x = width/3;
y = height /2;
w = 250;
h = 20;
//ball
ex = 20;
ey = height/2;
ew = 28;
er = ew/2;
speedx = 5;
speedy = 5;
}
void draw () {
background (0, 255, 255);
x = mouseX;
/* if (clicked == true) {
xx = xx+speed;
} else {
}
if (ball == true) {
fill (#ffffff);
rect (x , y, buttonwidth, buttonheight);
} */
if (clicked == true) {
background (255, 0, 255);
} else {
clicked = false;
}
// bouncing off top and bottom
if ( (ey > height) || (ey < 0) ) {
speedy = speedy * -1;
}
// bouncing off sides
if ( (ex > width) || (ex < 0) ) {
speedx = speedx * -1;
}
// is the ball touching the button at all
if ( (ex + er >= x ) && (ex - er <= x + w) && (ey + er >= y) &&
(ey - er <= y + h) ) {
// if it's touching the button on the left side
if (ex < x) {
speedx = -3.5;
//ex = ex - speedx;
} else if (ex > x + w) {
speedx = 3.5;
//ex = ex + speedx;
} else if (ey < y) {
speedy = -3.5;
//ey = ey - speedy;
} else if (ey > y + h) {
speedy = 3.5;
//ey = ey +speedy;
}
clicked = !clicked;
}
fill (#fcd805);
ellipse (ex, ey, ew, ew);
fill (255, 0, 0 );
rect (x, y, w, h);
ey = ey + speedy;
ex = ex + speedx;
}