0

I want to move background vertically.I tried it but it's not working.By applying below code image background remain still.I tried it by changing it's direction here (bgEntity = new VerticalParallaxEntity(0.0f, background, direction)) but cause no effect.

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);

I used this class :

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
        // ===========================================================
    }
}

Any suggestions? Thanks in advance!

user2952423
  • 121
  • 7

2 Answers2

1

I can't add a comment to the previous answer so I'll just add to it here.

You need to use AutoVerticalParallaxBackground. This is not provided by default with the game engine but AndAppsUK has created the classes for us (both AutoVerticalParallaxBackground and VerticalParallaxBackground) - http://www.andengine.org/forums/post306324.html#p31347

After you have added the class to your code, simply change this line

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

to (notice the extra argument for the change speed and the different class)

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

Also, change the speed value on your VerticalParallaxEntity to like 0.2f

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

Then a little bit of experimentation should get you the result you want.

Hope this helps.

sij_a
  • 633
  • 5
  • 10
0

It looks like you've done everything right. Just increase the parallax factor here:

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

0.0f - is no factor so it doesn't move

You can also try

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

where the last argument are changes per sec

Mateusz Gaweł
  • 673
  • 1
  • 8
  • 22
  • by using above suggestion nothing happened.Background is not moving. and i don't have any constructor which has these parameter..(new VerticalParallaxBackground(0, 0, 0, 5);) – user2952423 May 26 '14 at 07:18