如何对实体执行 hitTest

发布于 2024-12-18 05:39:41 字数 62 浏览 1 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-12-25 05:39:41

您需要为指针事件添加一个侦听器。下面是实现这一目标的简单方法:

...
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!");
        }

    }

    ...

}

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!");
        }

    }

    ...

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文