PlayN Game Library Tutorial #2 – Handling mouse clicks and touch events

In this tutorial let’s explore how to get mouse click events and simple handheld devices touch events.
We can capture a start and end of each event by defining a listener in the init() method.
Each click can call onPointerStart(), when the button is pressed and onPointerEnd() when the button is released.
The Pointer.Event object contains the x and y coordinates.

The example bellow is an extension of the code from Tutorial #1, the code to draw the ball was re-factored into a separate method drawGradientBall().
When the ball is clicked, it’s color is changed.
When a click is registered, the distance between the x,y coordinates of the event and the ball are calculated and if the distance is smaller than the radius the ball is redrawn with a different color. Note the call to pointer().setListener .

Leave a Reply

Your email address will not be published. Required fields are marked *