Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

1 Answer

up vote 1 down vote accepted

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() {
    //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.
    scene.attachChild(background);

    //tell the scene to sort itself so the Z indices are correct.
    scene.sortChildren();
}

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.