如何在实体上执行hitTest

时间:2011-11-27 02:44:10

标签: playn

当用户点击移动或静态对象(即实体)时,如何收听鼠标单击事件并执行hitTest以查看是否在目标上单击了鼠标?

1 个答案:

答案 0 :(得分: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!");
        }

    }

    ...

}