Reputation: 1
I am making the game Breakout and want to move the paddle by it following my mouse-movements. When the mouse leaves my AnchorPane
named windowPane
, the paddle is halfway gone, too. I would like to ask for help: how do I make the paddle not follow the mouse any longer when I reach the bounds of my pane?
windowPane.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
/**move Paddle with mouse *@author Can @author SziSzi */
paddle.setLayoutX(event.getY());
paddle.setLayoutX(event.getX()- paddle.getWidth()/2);
}
});
Upvotes: 0
Views: 71
Reputation: 82461
Check, if the position would make the paddle violate the bounds of the pane:
double h = paddle.getHeight();
double pW = paddle.getWidth() / 2;
double x = event.getX();
double y = event.getY();
if (x >= pW && x + pW <= windowPane.getWidth() && y + h <= windowPane.getHeight()) {
paddle.setLayoutY(y);
paddle.setLayoutX(x - pW);
} // otherwise ignore the event
Upvotes: 1