At work with the class

Hi everyone I need a confirmation or a hand for a game: I am working with colored buttons and I need to understand if when I step on with the mouse they are selected and then transported within my sketch.
Can anyone tell me if how I did it could be correct?

float posOriginaleX;
float posOriginaleY;
color verde_color = (#12E300); 
color viola_color = (#E200E3);
color arancio_color = (#E38100);

Bottone Bottone_verde;
Bottone Bottone_viola;
Bottone Bottone_arancio;


void setup() {
  size(600, 600);
  posOriginaleX = 160;
  posOriginaleY = 20;
  Bottone_verde = new Bottone(posOriginaleX, posOriginaleY, 2, 0, 255, color (0, 255, 0)); 
  Bottone_viola = new Bottone(posOriginaleX, posOriginaleY, 1, 0, 255, color(255, 0, 255));
  Bottone_arancio = new Bottone(posOriginaleX, posOriginaleY, 3, 0, 255, color(255, 117, 20));
}

void draw() {
  background(0);

  Bottone_verde.display();
  Bottone_verde.update();


  Bottone_viola.display();
  Bottone_viola.update();


  Bottone_arancio.display();
  Bottone_arancio.update();
}

void mouseDragged() {
  if (mouseButton == LEFT) {
  }
}
class Bottone {
  float posOriginaleX, posOriginaleY;
  float posX, posY;
  int size, min, max;
  color c;
  //Bottone Bottone;

  Bottone(float xPosOrigin, float yPosOrigin, float xpos, float ypos, int s, color col) {
    posOriginaleX = xPosOrigin;
    posOriginaleY = yPosOrigin;
    posX = xpos;
    posY = ypos;
    size = s;
    c = col;
  }

  void display() {
    noStroke();
    fill(verde_color);
    rect (posOriginaleX, posOriginaleY, 80, 80);
    fill(viola_color);
    rect (posOriginaleX+100, posOriginaleY, 80, 80);
    fill(c);
    rect (posOriginaleX+200, posOriginaleY, 80, 80);
  }

  void update() {
    isInside();
  }

  boolean isInside() {
    if (mouseX >= posOriginaleX && mouseX <= posOriginaleX && mouseY >= posOriginaleY && mouseY <= posOriginaleY) return true;
    else return false;
  }
}


Thanks

1 Like

Hi,

Could you explain a little bit more? Do you mean draggable?

2 Likes

Si esatto devo riuscire a spostare i miei quadratini all’interno del mio sketch, quindi verificare quando il mio mouse è sul quadratino e quando no.

This example should help with determining when mouse is over the rectangle areas:

https://processing.org/examples/rollover.html

And to drag the rectangle(s), x and y variables would be mouseX and mouseY.

3 Likes

Hello !

When I look into your class:

please read again the tutorial about objects. See Objects / Processing.org

These lines above are wrong.

They show the 3 buttons INSIDE the class.

But the class has the purpose to represent only ONE button and to display only one button. He doesn’t know about the other buttons.

The class is the cookie maker, the objects (your 3 buttons) are the cookies.

The correct way

The correct way is in your draw() function:

Correct is calling each button separately. (maybe update before display).

In setup()

but alas, the way you build the buttons in setup is not correct, because they all have the same position…

You want

  Bottone_verde = new Bottone(posOriginaleX, posOriginaleY, 2, 0, 255, color (0, 255, 0)); 
  Bottone_viola = new Bottone(posOriginaleX+100, posOriginaleY, 1, 0, 255, color(255, 0, 255));
  Bottone_arancio = new Bottone(posOriginaleX+200, posOriginaleY, 3, 0, 255, color(255, 117, 20));

or so…

Chrisir :wink:

3 Likes