我应该如何为 AndEngine 实现 ITouchArea.convertSceneToLocalCooperatives() ?

发布于 2024-12-05 08:23:59 字数 910 浏览 7 评论 0原文

我正在使用 AndEngine,并且正在创建一个管理一堆精灵的类。 该类需要在用户触摸它时执行一些操作,因此我使其实现 ITouchArea 接口。

我定义了方法 contains:

 @Override
 public boolean contains(float pX, float pY) {
    if(     pX >= this.mXCenterPosition - X_DIMENSION/2 &&
            pX <= this.mXCenterPosition + X_DIMENSION/2 && 
            pY >= this.mYCenterPosition - Y_DIMENSION/2 &&
            pY <= this.mYCenterPosition + Y_DIMENSION/2)
        return true;
    return false;
 }

和这个方法:

@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
        float pTouchAreaLocalX, float pTouchAreaLocalY)

我仍然怀念的是:

public float[] convertSceneToLocalCoordinates(float pX, float pY)

如果没有定义它,或者返回 null,程序就会崩溃。我试图看看它在其他类中是如何实现的,但是我并没有真正理解它是做什么的,也不知道它的功能是什么,所以我不知道如何实现它。类的区域是一个简单的矩形。

这个方法应该做什么?我将如何实施它?

I'm using AndEngine and I'm creating a class that manages a bunch of sprites.
The class needs to perform some actions when the user touches it, so I made it implement the ITouchArea interface.

I defined the method contains:

 @Override
 public boolean contains(float pX, float pY) {
    if(     pX >= this.mXCenterPosition - X_DIMENSION/2 &&
            pX <= this.mXCenterPosition + X_DIMENSION/2 && 
            pY >= this.mYCenterPosition - Y_DIMENSION/2 &&
            pY <= this.mYCenterPosition + Y_DIMENSION/2)
        return true;
    return false;
 }

and this method:

@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
        float pTouchAreaLocalX, float pTouchAreaLocalY)

What I still miss is this:

public float[] convertSceneToLocalCoordinates(float pX, float pY)

Without defining it, or returning null, the program crashes. I tried to look at how it is implemented in other classes, but I didn't really understood what it does, and I don't know what it is its function, so I don't know how to implement it. The area of the class is a simple rectangle.

What should this method do? How would I implement it?

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

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

发布评论

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

评论(1

‖放下 2024-12-12 08:23:59

该方法应该将场景空间中的坐标转换为本地空间(实体的)。如果您的 Entity 上只有翻译,那么您只需分别从给定的 x 和 y 坐标中减去 mXmY 即可。

[图中,实体的(mX, mY)为(300, 100)]
在此处输入图像描述

对于旋转和缩放,它将使用相同的概念。只是 x 轴和 y 轴会旋转/缩放,因此 Sprite 也会旋转/缩放。您可能希望使用 Transformation 对象来实现此功能(与 Entity 的方式相同)。请参阅 Entity.convertLocalToSceneCooperatives(final float pX, Final float pY, Final float[] pReuse)。

The method should be converting a coordinate in scene space to local space (of the Entity). If there's only translation on your Entity, then you would simply subtract mX and mY from the given x and y coordinates respectively.

[In the image, the entity's (mX, mY) is (300, 100)]
enter image description here

With rotation and scale it'll be using the same concepts. It's just that the x and y axes will be rotated/scaled, and thus the Sprite will also be rotated/scaled. You'll probably want to implement this using a Transformation object (the same way Entity does). See Entity.convertLocalToSceneCoordinates(final float pX, final float pY, final float[] pReuse).

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