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;
}
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • e-mail
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

Leave a Reply