我应该如何为 AndEngine 实现 ITouchArea.convertSceneToLocalCooperatives() ?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该方法应该将场景空间中的坐标转换为本地空间(实体的)。如果您的
Entity
上只有翻译,那么您只需分别从给定的 x 和 y 坐标中减去mX
和mY
即可。[图中,实体的(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 yourEntity
, then you would simply subtractmX
andmY
from the given x and y coordinates respectively.[In the image, the entity's (mX, mY) is (300, 100)]

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 wayEntity
does). SeeEntity.convertLocalToSceneCoordinates(final float pX, final float pY, final float[] pReuse)
.