How do I create both a parallax background and an autoparallax background in the same scene using ?

The problem is, I want one entity to move at a constant speed regardless of the camera's movement, and I want another entity to move depending on the camera's movement.

To do it, I created a parallax background and an autoparallax background. The problem is, I can show these two background separately by doing mScene.setBackground(background); But I want these two backgrounds to show at the same time.

  • \$\begingroup\$ If the answer below was helpful to you, please consider accepting it as an answer (the check mark to the left). Doing so helps encourage participation in the community. Plus, in that it's your first time accepting an answer, I believe you'll receive a badge. \$\endgroup\$ Commented Feb 28, 2013 at 9:24

1 Answer 1


Instead of using mScene.setBackground(background) take a look at background.attachParallaxEntity(). The AndEngine example for autoparallax is a good example on how to do this. Here's the critical part:

final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerBack.getHeight(), this.mParallaxLayerBack, vertexBufferObjectManager)));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f, new Sprite(0, 80, this.mParallaxLayerMid, vertexBufferObjectManager)));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerFront.getHeight(), this.mParallaxLayerFront, vertexBufferObjectManager)));

In that example, you can see it's using three different relative "speeds" relative to the camera movement. An alternative approach, in light of your comment that you'd like something to move at a constant speed, is to not think of it as officially a "background," but just treat it as a sprite, with a z-index behind your "foreground" entities, and use a MoveModifier to move the sprite at a constant speed. Here's what I mean:

private void moveBackground() {
    //create your sprite
    Sprite background = new Sprite(params);

    //Give it movement.
    background.registerEntityModifier(new MoveModifier(params...));

    //Set its z-index to behind your entities.
    background.setZIndex([some number lower than your foreground]);

    //attach it to your scene.

    //tell the scene to sort itself so the Z indices are correct.

Don't forget when attaching or removing sprites that it must be done in one of the main threads.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .