The Draggable Box – Take 3

Just a minor fix to the draggable box I made the other day. Previously it positioned the box so that the top-left corner was aligned with the mouse. Now, it centres the box horizontally and vertically behind the mouse.

It is available online here along with the full source code.

The changes made to fix this are pretty simple, and the code changes are shown below.


I replaced:

if (mousePressed && (mouseButton == LEFT)) {
boxX = mouseX;
boxY = mouseY;
boxWidth += x2;
boxHeight += y2;
}

With:

if (mousePressed && (mouseButton == LEFT)) {
boxX = mouseX - (boxWidth/2);
boxY = mouseY - (boxHeight/2);
boxWidth += x2;
boxHeight += y2;
}

Tags: , ,

Leave a Reply