Dragging a box - beginner
in
Programming Questions
•
2 years ago
Hi - I am new to Processing, so please bear with me.
My first attempt at a sketch is intended to allow a box to be dragged around the screen. It works OK until I try to drag the box rapidly and then it gets left behind. I would be grateful for any suggestions as to where I have gone wrong.
Code:
My first attempt at a sketch is intended to allow a box to be dragged around the screen. It works OK until I try to drag the box rapidly and then it gets left behind. I would be grateful for any suggestions as to where I have gone wrong.
Code:
- DragBox drgbx;
void setup()
{
size(700, 700);
background(210);
drgbx = new DragBox(25, 25, 100, 100);
}
void draw()
{
background(210);
drgbx.boxDisplay();
//move box with mouse
rect(drgbx.boxX, drgbx.boxY, drgbx.boxW, drgbx.boxH);
}
class DragBox
{
float boxX;
float boxY;
int boxW;
int boxH;
boolean msOver = false;
boolean msPressed = false;
// constructor
DragBox(float tmpBoxX, float tmpBoxY, int tmpBoxW, int tmpBoxH) {
boxX = tmpBoxX;
boxY = tmpBoxY;
boxW = tmpBoxW;
boxH = tmpBoxH;
}
boolean mOver()
{
if (mouseX >= boxX && mouseX <= boxX+boxW &&
mouseY >= boxY && mouseY <= boxY+boxH) {
msOver = true;
return msOver;
} else {
msOver = false;
return msOver;
}
}
boolean mPressed()
{
if (mousePressed) {
msPressed = true;
return msPressed;
} else {
msPressed = false;
return msPressed;
}
}
void boxDisplay() {
//code to display box in specific location
mOver();
mPressed();
if (msOver) {
if (msPressed) {
//println("Mouse Over and Mouse Pressed");
//calculate mouse movement and move box the same amount
float posXDif = mouseX - pmouseX;
float posYDif = mouseY - pmouseY;
//update local positional variables based on mouse location
boxX = boxX + posXDif;
boxY = boxY + posYDif;
}
}
}
}
1