当用户点击一个移动的或静态的对象,即一个实体时,如何监听鼠标点击事件并执行hitTest来查看鼠标是否点击了目标?
问问题
525 次
1 回答
2
您需要为指针事件添加一个侦听器。以下是实现这一目标的简单方法:
...
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!");
}
}
...
}
于 2011-11-27T12:20:10.567 回答