Andengine - 将精灵附加到视差背景仍然会导致它重复

Andengine - Attaching sprite to parallax background still causes it to repeat

我正在 AndEngine 中开发游戏,我想将精灵附加到视差背景(在我的主菜单上),但我不想重复精灵(这是当前的发生)。

我已经尝试过这个(下面),但是我在游戏中使用了精灵,所以当我回到主菜单时,精灵会移动(我尝试重置精灵,但似乎没有工作)。

Sprite playerCar = new Sprite(playerX, playerY,
            mResourceManager.mPlayerCarTextureRegion,
            mVertexBufferObjectManager);
    playerCar.setRotation(-15);
    attachChild(playerCar);

我想做的是:

将我的精灵定义为正常:

Sprite playerCar = new Sprite(playerX, playerY,
            mResourceManager.mPlayerCarTextureRegion,
            mVertexBufferObjectManager);
    playerCar.setRotation(-15);

然后附在我的背景上:

ParallaxBackground menuParallaxBackground = new ParallaxBackground(0,
            0, 0);

    menuParallaxBackground.attachParallaxEntity(new ParallaxEntity(0,
            new Sprite(0, SCREEN_HEIGHT
                    - mResourceManager.mParallaxLayerRoad.getHeight(),
                    mResourceManager.mParallaxLayerRoad,
                    mVertexBufferObjectManager)));

    menuParallaxBackground.attachParallaxEntity(new ParallaxEntity(0,
            playerCar));

这也行,但汽车一直重复我不想要的。

如有任何帮助,我们将不胜感激!谢谢

问题出在 ParallaxBackground class 中的 ParallaxEntity class 上的 onDraw 方法。在绘制 sprite 的地方有一个循环,一直循环直到 sprite 填满屏幕的宽度。我只是创建了一个自定义 class 并删除了 while 循环,这样我就可以将它用于我的主菜单。

ParallaxEntity onDraw 之前的代码:

public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValue) {
        pGLState.pushModelViewGLMatrix();
        {
            final float cameraWidth = pCamera.getWidth();
            final float shapeWidthScaled = this.mAreaShape.getWidthScaled();
            float baseOffset = (pParallaxValue * this.mParallaxFactor) % shapeWidthScaled;

            while(baseOffset > 0) {
                baseOffset -= shapeWidthScaled;
            }
            pGLState.translateModelViewGLMatrixf(baseOffset, 0, 0);

            float currentMaxX = baseOffset;

            do {
                this.mAreaShape.onDraw(pGLState, pCamera);
                pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
                currentMaxX += shapeWidthScaled;
            } while(currentMaxX < cameraWidth);
        }
        pGLState.popModelViewGLMatrix();
    }

我的 CustomParallaxEntity onDraw 代码之后(注意删除了最后一个 while 循环):

public void onDraw(final GLState pGLState, final Camera pCamera,
            final float pParallaxValue) {
        pGLState.pushModelViewGLMatrix();
        {
            final float shapeWidthScaled = this.mAreaShape.getWidthScaled();
            float baseOffset = (pParallaxValue * this.mParallaxFactor)
                    % shapeWidthScaled;

            while (baseOffset > 0) {
                baseOffset -= shapeWidthScaled;
            }
            pGLState.translateModelViewGLMatrixf(baseOffset, 0, 0);

            this.mAreaShape.onDraw(pGLState, pCamera);
            pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
        }
        pGLState.popModelViewGLMatrix();
    }

感谢对我的问题的评论帮助我找出解决方案。