Why do my buttons keep getting assimilated?
in
Programming Questions
•
2 years ago
hi
can any please tell me why my when i drag a button over another one in this sketch it attaches itself to the button i'm dragging? all i want to do is drag the buttons around freely and not have them effect each other.
some help would be greatly appreciated, thanks!
can any please tell me why my when i drag a button over another one in this sketch it attaches itself to the button i'm dragging? all i want to do is drag the buttons around freely and not have them effect each other.
some help would be greatly appreciated, thanks!
- int totBtn = 4;
Button[] name = new Button[totBtn];
void setup(){
size(700,550);
background(127);
for(int i = 0; i < totBtn; i++){
name[i] = new Button(color(255, 255, 255), 10 , 70 + i*40, 200, 40, "buttonName" + i);
}
}
void draw(){
background(127);
for(int i = 0; i < totBtn; i++){
if(name[i].myButtonPressed){
name[i].xLocB = mouseX - name[i].xSizeB/2;
name[i].yLocB = mouseY - name[i].ySizeB/2;
name[i].disp();
name[i].press();
}else{
//text("button is not currently being pressed"+ name[i],250,height/2 + 15+ i*40);
name[i].disp();
name[i].over();
name[i].press();
//println(name[i].myButtonPressed);
}
}
}
class Button{
// declare variables used in the Button class here
color cB;
float xLocB;
float yLocB;
float xSizeB;
float ySizeB;
String nameB;
boolean myButtonOver;
boolean myButtonPressed;
// Temporary variable assignment
Button(color tempcB, float tempxLocB, float tempyLocB, float tempxSizeB, float tempySizeB,String tempNameB){
cB = tempcB;
xLocB = tempxLocB;
yLocB = tempyLocB;
xSizeB = tempxSizeB;
ySizeB = tempySizeB;
nameB = tempNameB;
}
boolean over(){
if(mouseX >= xLocB && mouseX <= xLocB + xSizeB && mouseY >= yLocB && mouseY <= yLocB + ySizeB && nameB == nameB ){
myButtonOver = true;
return myButtonOver;
}else{
myButtonOver = false;
return myButtonOver;
}
}
boolean press(){
if(myButtonOver && mousePressed ){
myButtonPressed = true;
return myButtonPressed;
}else{
myButtonPressed = false;
return myButtonPressed;
}
}
void disp(){
fill(cB);
rect(xLocB, yLocB,xSizeB, ySizeB);
fill(cB/2);
text(nameB, (xSizeB/2)+xLocB-((xSizeB/2)/2), (ySizeB/2)+yLocB+((ySizeB/2)/2));
fill(cB);
}
}
1