如何在屏幕的特定部分添加精灵 AndEngine

发布于 2024-12-14 04:29:55 字数 606 浏览 4 评论 0原文

我使用这种方法随机添加一个精灵到屏幕上。

 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 技术交流群。

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

发布评论

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

评论(2

狼性发作 2024-12-21 04:29:55

首先,实体的 (x,y) 坐标位于其左上角。因此,minY 应该是 0,或者您可以这样做:

float y = rand.nextFloat(maxY);

您不需要所有这些 (int) 转换,您可以删除他们。

为了使位置随机,也获得一个随机的 X:

float maxX = this.mCamera.getWidth() - this.mBallTextureRegion.getWidth();
float x = rand.nextFloat(maxX);

这应该可行。

First of all, the (x,y) coordinates of an entity are on it's top left. So, minY should be 0, or you could just do:

float y = rand.nextFloat(maxY);

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:

float maxX = this.mCamera.getWidth() - this.mBallTextureRegion.getWidth();
float x = rand.nextFloat(maxX);

This should work.

浅忆 2024-12-21 04:29:55

在android屏幕上,坐标系的原点在左上角。

(0,0)......(1,0)

(0,1)......(1,1)

因此,如果您希望某些内容始终在屏幕顶部生成,那么 Y 将需要为 0。始终。 X 值可以是从 0 到宽度的任意值。这会将对象放置在顶部,并在 X(左右)方向上随机放置

On the android screen, the origin of the coordinate system is in the upper left corner.

(0,0)......(1,0)

(0,1)......(1,1)

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

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