When user clicks on a moving or static object, ie an entity, how to listen to a mouse click event and perform a hitTest to see if the mouse was clicked on the target?
Asked
Active
Viewed 527 times
1 Answers
2
You need to add a listener to the pointer events. Below is a simple way to achieve that:
...
import playn.core.Pointer;
public class HitTestGame implements Game
{
@Override
public void init()
{
...
final HitTestGame self = this;
// pointer
pointer().setListener(new Pointer.Adapter() {
@Override
public void onPointerEnd(Pointer.Event event)
{
self.onPointerUp((int)event.x(), (int)event.y());
}
//public void onPointerStart(Pointer.Event event)
//public void onPointerDrag(Pointer.Event event)
});
...
}
public void onPointerUp(int x, int y)
{
// Do region checks here
if ( (x >= entity.left() && x <= entity.right())
&& (y >= entity.top() && y <= entity.bottom()) )
{
System.out.println("Entity has been clicked!");
}
}
...
}

prageeths
- 148
- 1
- 8