# Showing Parallax entity and AutoParallax entity on same scene in AndEngine

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.

• 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. – Cameron Fredman Feb 28 '13 at 9:24

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)));
scene.setBackground(autoParallaxBackground);


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() {
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]);