在andengine中垂直移动背景

时间:2014-05-19 06:37:11

标签: android andengine

我想垂直移动背景。我尝试过但它不起作用。通过应用下面的代码图像背景仍然是静止的。我通过改变它的方向来尝试它(bgEntity = new VerticalParallaxEntity(0.0f,background,direction))但是没有效果。

autoParallaxBackground = new VerticalParallaxBackground(0, 0, 0);
background = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT,
        this.mParallaxLayerBack, this.vbom);
bgEntity = new VerticalParallaxEntity(0.0f, background, 1);

autoParallaxBackground.attachVerticalParallaxEntity(bgEntity);
autoParallaxBackground.attachVerticalParallaxEntity(bgEntity);
mainScene.setBackground(autoParallaxBackground);

我用过这个课:

public class VerticalParallaxBackground extends ParallaxBackground {
    public static int SCROLL_DOWN = -1;
    public static int SCROLL_UP = 1;
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================

    private final ArrayList<VerticalParallaxEntity> mParallaxEntities = new ArrayList<VerticalParallaxEntity>();
    private int mParallaxEntityCount;

    protected float mParallaxValue;

    // ===========================================================
    // Constructors
    // ===========================================================

    public VerticalParallaxBackground(float red, float green, float blue) {
        super(red, green, blue);
        // TODO Auto-generated constructor stub
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    public void setParallaxValue(final float pParallaxValue) {
        this.mParallaxValue = pParallaxValue;
    }

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public void onDraw(final GLState pGLState, final Camera pCamera) {
        super.onDraw(pGLState, pCamera);

        final float parallaxValue = this.mParallaxValue;
        final ArrayList<VerticalParallaxEntity> parallaxEntities = this.mParallaxEntities;
        // Log.d("VAPB", "VAPB onDraw pre entity");
        for (int i = 0; i < this.mParallaxEntityCount; i++) {
            parallaxEntities.get(i).onDraw(pGLState, pCamera, parallaxValue);
        }
    }

    // ===========================================================
    // Methods
    // ===========================================================

    public void attachVerticalParallaxEntity(
            final VerticalParallaxEntity pParallaxEntity) {
        this.mParallaxEntities.add(pParallaxEntity);
        this.mParallaxEntityCount++;
    }

    public boolean detachVerticalParallaxEntity(
            final VerticalParallaxEntity pParallaxEntity) {
        this.mParallaxEntityCount--;
        final boolean success = this.mParallaxEntities.remove(pParallaxEntity);
        if (success == false) {
            this.mParallaxEntityCount++;
        }
        return success;
    }

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================

    public static class VerticalParallaxEntity {
        // ===========================================================
        // Constants
        // ===========================================================

        // ===========================================================
        // Fields
        // ===========================================================

        final float mParallaxFactor;
        final IAreaShape mShape;
        private int direction;

        // ===========================================================
        // Constructors
        // ===========================================================

        public VerticalParallaxEntity(final float pParallaxFactor,
                final IAreaShape pShape) {
            this.mParallaxFactor = pParallaxFactor;
            this.mShape = pShape;
            this.direction = VerticalParallaxBackground.SCROLL_DOWN;
        }

        public VerticalParallaxEntity(final float pParallaxFactor,
                final IAreaShape pShape, int direction) {
            this.mParallaxFactor = pParallaxFactor;
            this.mShape = pShape;
            this.direction = direction;
        }

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================

        // ===========================================================
        // Methods
        // ===========================================================

        public void onDraw(final GLState pGL, final Camera pCamera,
                final float pParallaxValue) {

            pGL.pushModelViewGLMatrix();
            final float cameraHeight = pCamera.getHeight();
            final float shapeHeightScaled = this.mShape.getHeightScaled();
            float baseOffset = (pParallaxValue * this.mParallaxFactor)
                    % shapeHeightScaled;
            while (baseOffset > 0) {
                baseOffset -= shapeHeightScaled;
            }
            pGL.translateModelViewGLMatrixf(0, (direction * baseOffset), 0);
            float currentMaxY = baseOffset;
            do {
                this.mShape.onDraw(pGL, pCamera);
                pGL.translateModelViewGLMatrixf(0,
                        (direction * shapeHeightScaled), 0);
                currentMaxY += shapeHeightScaled;
            } while (currentMaxY < (cameraHeight + shapeHeightScaled));
            // Added shapeHeightScaled to cameraHeight so the drawing flows in
            // instead of popping in.

            pGL.popModelViewGLMatrix();

        }
        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
    }
}

有什么建议吗? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

我无法在上一个答案中添加评论,所以我只想在此处添加评论。

您需要使用AutoVerticalParallaxBackground。游戏引擎默认不提供此功能,但AndAppsUK已为我们创建了类(AutoVerticalParallaxBackground和VerticalParallaxBackground) - http://www.andengine.org/forums/post306324.html#p31347

将类添加到代码后,只需更改此行

即可
autoParallaxBackground = new VerticalParallaxBackground(0, 0, 0);

to(注意更改速度和不同类的额外参数)

autoParallaxBackground = new AutoVerticalParallaxBackground(0, 0, 0, 15);

此外,将VerticalParallaxEntity上的速度值更改为0.2f

bgEntity = new VerticalParallaxEntity(0.2f, background, 1);

然后进行一些实验可以得到你想要的结果。

希望这有帮助。

答案 1 :(得分:0)

看起来你已经做好了一切。只需增加视差因子:

bgEntity = new VerticalParallaxEntity(0.0f, background, 1);

0.0f - 没有因素,所以它不会移动

您也可以尝试

new VerticalParallaxBackground(0, 0, 0, 5);

其中最后一个参数是每秒更改