When i try code for moving paddle setting location x = getX(event) -getWidth(paddle)/2; but with this a part of my paddle goes out of window. What should i do ?

2 Answers 2


Are you doing anything with x ? Checking it to make sure that you don't go outside (ie, checking that it is within the bounds of width of the window?) And when you do that, make sure you take into account the width of the paddle itself.

Let's say that window is 400px wide and your paddle is 10px wide.

If you move your mouse to the very edge, getX(event) will be 400 (or very close).

400 - 10/2 = 395

But do you want the paddle to go that far? at x = 395, 5 pixels of it has disappeared off the screen, because the location is based on the top-left corner of the rectangle.

What I did was create a constant called MAXPADDLEX that used the window width and paddle width to come up with the maximum x value that I would want.

Hope that helps, Brenda.

  • thanks it worked
    – Mercury
    Jun 11, 2014 at 6:29
  • Glad to have helped. :)
    – curiouskiwi
    Jun 11, 2014 at 6:34

an 'if' test might help determine whether to 'setLocation()' based on whether your cursor is within a boundary

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .