如何更改精灵在场景中随机出现的位置?

发布于 2024-12-13 19:56:02 字数 1344 浏览 8 评论 0原文

我将此方法与 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;


             face = new Sprite(x, y, this.mBallTextureRegion.clone());
             body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);

      this.mScene.attachChild(face);
     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
             int minDuration = 2;
            int maxDuration = 4;

            int rangeDuration = maxDuration - minDuration;
            int actualDuration = rand.nextInt(rangeDuration) + minDuration;

            MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth());

            face.registerEntityModifier(mod);

我想要做的是

,我希望将其添加到顶部并落下,而不是选择随机位置并将精灵添加到场景的左侧。

我基本上想翻转方向 我就是不知道怎么办。我尝试的一切都不走运。

有什么想法或建议吗?

I am using this method with AndEngine to add a sprite to the screen and make it move across the screen.

 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;


             face = new Sprite(x, y, this.mBallTextureRegion.clone());
             body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);

      this.mScene.attachChild(face);
     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
             int minDuration = 2;
            int maxDuration = 4;

            int rangeDuration = maxDuration - minDuration;
            int actualDuration = rand.nextInt(rangeDuration) + minDuration;

            MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth());

            face.registerEntityModifier(mod);

}

What i would like to do is, instead of the Random position being selected and the sprite being added to the left side of the Scene, i would like for it to be added to the Top and fall down.

I basically want to flip the direction,
I just cant figure out how. Everything i tried was no luck.

Any ideas or suggestions?

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

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

发布评论

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

评论(1

携君以终年 2024-12-20 19:56:02

虽然我对 Andengine 不太了解,但我怀疑您想要更改这些行:

float x = (int) mCamera.getHeight() - mBallTextureRegion.getHeight();
float y = rand.nextInt((int)rangeY) + minY;

MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth());

翻转 x 和 y 语句,但如果您希望它出现在屏幕之外,请将 y 设置为 -mBallTextureRegion.getHeight() 。

对于MoveXModifier,我猜有一个相应的MoveYModifier(分别使用face.getY()和-face.getHeight())

While I don't know much about Andengine, I suspect you would want to change these lines:

float x = (int) mCamera.getHeight() - mBallTextureRegion.getHeight();
float y = rand.nextInt((int)rangeY) + minY;

MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth());

Flip the x and y statements, but set y to -mBallTextureRegion.getHeight() if you want it to appear outside the screen.

For the MoveXModifier, I would guess there is a corresponding MoveYModifier (use face.getY() and -face.getHeight() respectively)

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