如何在屏幕的特定部分添加精灵 AndEngine
我使用这种方法随机添加一个精灵到屏幕上。
private void addFace() {
Random rand = new Random();
float x = (int) mCamera.getHeight() + mBallTextureRegion.getHeight();
float minY = mBallTextureRegion.getHeight();
float maxY = (int)(mCamera.getWidth() - mBallTextureRegion.getWidth());
float rangeY = maxY - minY;
float y = rand.nextInt((int)rangeY) + minY;
this.mFaceCount++;
Log.e("Faces: ", "Face" + this.mFaceCount);
Sprite face = null;
Body body = null;
唯一的问题是我希望将精灵添加到屏幕顶部(即相机),但它们却添加到屏幕的一侧。
关于如何执行此操作有什么建议吗?
I use this method to add a Sprite to the screen randomly.
private void addFace() {
Random rand = new Random();
float x = (int) mCamera.getHeight() + mBallTextureRegion.getHeight();
float minY = mBallTextureRegion.getHeight();
float maxY = (int)(mCamera.getWidth() - mBallTextureRegion.getWidth());
float rangeY = maxY - minY;
float y = rand.nextInt((int)rangeY) + minY;
this.mFaceCount++;
Log.e("Faces: ", "Face" + this.mFaceCount);
Sprite face = null;
Body body = null;
The only problem is i would like for the sprites to be added at the top of the screen(which is the camera) but instead they are added on the side of the screen.
Any suggestions on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,实体的 (x,y) 坐标位于其左上角。因此,
minY
应该是0
,或者您可以这样做:您不需要所有这些
(int)
转换,您可以删除他们。为了使位置随机,也获得一个随机的 X:
这应该可行。
First of all, the (x,y) coordinates of an entity are on it's top left. So,
minY
should be0
, or you could just do:You don't need all of these
(int)
casts, you can delete them.In order to make to position random, get a random X too:
This should work.
在android屏幕上,坐标系的原点在左上角。
因此,如果您希望某些内容始终在屏幕顶部生成,那么 Y 将需要为 0。始终。 X 值可以是从 0 到宽度的任意值。这会将对象放置在顶部,并在 X(左右)方向上随机放置
On the android screen, the origin of the coordinate system is in the upper left corner.
So if you want something to always spawn at the top of the screen, then Y will need to be 0. Always. the X value can be anything from 0 to the width, randomly. This will place the object at the top, and randomly in the X (left-right) direction