move as one
in
Programming Questions
•
1 years ago
is there a way to avoid overlapping circles being moved together?..
- Button[] buttons= new Button[20];
- void setup() {
- size(500, 500);
- smooth();
- for (int i=0; i<buttons.length;i++) {
- buttons[i]= new Button(random(width), random(height), random (20, 80), color(random(255), random (255), random(255)));
- }
- }
- void draw() {
- background(255);
- for (int i=0; i<buttons.length;i++) {
- buttons[i].display();
- }
- }
- void mousePressed() {
- for (int i=0; i<buttons.length;i++) {
- buttons[i].rollOver();
- }
- }
- void mouseDragged() {
- for (int i=0; i<buttons.length;i++) {
- buttons[i].mouseDragged();
- }
- }
- void mouseReleased() {
- for (int i=0; i<buttons.length;i++) {
- buttons[i].mouseReleased();
- }
- }
- class Button {
- float x;
- float y;
- float radius;
- color c;
- boolean click=false;
- boolean drag= true;
- Button(float _x, float _y, float _radius, color _c) {
- x=_x;
- y=_y;
- radius=_radius;
- c=_c;
- }
- void display() {
- if (click) {
- fill (255, 0, 0);
- }
- else {
- fill(c);
- }
- ellipse (x, y, radius, radius);
- }
- void rollOver() {
- if (sqrt(sq(mouseX-x)+sq(mouseY-y))<=radius&&mousePressed ) {
- click=!click;
- drag=!drag;
- }
- }
- void mouseDragged() {
- if (drag) {
- x=mouseX;
- y=mouseY;
- }
- }
- void mouseReleased() {
- drag=false;
- }
- }
1