Almost.
BUT some remarks
-
in mousePressed() you have this:
if (dist(mouseX, mouseY, myDisc1.dragX, myDisc1.dragY) <= myDisc1.diam/2) {
That looks complicate: A lot of references to one object is an indicator that we want to have this in the class. Also, both lines almost are the same.
Eg. make a method over
inside the class.
And then in mousePressed() :
if(myDisc1.over) myFlag = 1;
-
In the class you have this:
if (myFlag == 1) {
myDisc1.dragX = mouseX;
myDisc1.dragY = mouseY;
} else if (myFlag == 2) {
myDisc2.dragX = mouseX;
myDisc2.dragY = mouseY;
}
This is obviously not in the spirit of OOP (object oriented programming) and won’t work when you have 100 circles in an array. You also use an object declared on Sketch level (before setup()) inside the class. Bad, because the purpose of the class is to be reusable in another sketch and there, the global vars (objects) could have different names or be in an array. Then your class won’t work anymore. Don’t use objects in a class (objects of the class itself), just use the variables inside the class.
-
Next exercise for you could be to use 5 circles in an array.
Chrisir
Sketch addresses issue 2:
int myFlag = 0;
Disc myDisc1;
Disc myDisc2;
void setup() {
size(400, 400);
myDisc1 = new Disc(150);
myDisc2 = new Disc(250);
}
void draw() {
background(255);
myDisc1.display();
myDisc2.display();
}
// -----------------------------------------------------------------------
void mouseDragged() {
if (myFlag == 1) {
myDisc1.move();
} else if (myFlag == 2) {
myDisc2.move();
}
}
void mousePressed() {
if (dist(mouseX, mouseY, myDisc1.dragX, myDisc1.dragY) <= myDisc1.diam/2) {
myFlag = 1;
} else if (dist(mouseX, mouseY, myDisc2.dragX, myDisc2.dragY) <= myDisc2.diam/2) {
myFlag = 2;
} else {
myFlag = 0;
}
}
// ==============================================================
class Disc {
int dragX;
int dragY=200;
int diam = 32;
color colDisc = color (random(100, 190));
// The Constructor
Disc(int tempDragX) {
dragX = tempDragX;
}
void display() {
stroke(0);
fill(colDisc);
ellipse(dragX, dragY,
diam, diam);
}
void move() {
dragX = mouseX;
dragY = mouseY;
} // method
//
}//class
//