Search code examples
androidandroid-layoutandenginegame-enginescene

How Do I Build Multi-Layered Scenes with AndEngine?


I am a noob to android development and i am trying to learn how to use AndEngine. I want to construct a scene where a sprite appears from behind an object in the foreground and then disappears in a whack-a-mole fashion. I've reviewed every example project but can't find anything code that shows how to do this. any help is greatly appreciated.

Currently, I am trying to use a sprite to set the foreground scene to no avail.

@Override
    public Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());

        final Scene scene = new Scene();
        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);

        final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
        final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
        final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
        final PhysicsHandler physicsHandler = new PhysicsHandler(face);
        face.registerUpdateHandler(physicsHandler);

        scene.attachChild(face);

        final Sprite foreground = new Sprite(centerX, centerY, this.mFaceTextureRegion2, this.getVertexBufferObjectManager());
        final PhysicsHandler physicsHandler2 = new PhysicsHandler(foreground);
        face.registerUpdateHandler(physicsHandler2);

        scene.setChildScene(foreground); //<--Gives me error
    return scene;
    }

Solution

  • If I understood correctly, you need layers.. You can use entities as layers:

    Entity backgroundLayer = new Entity();
    backgroundLayer.attachChild(face);
    
    Entity foregroundLayer= new Entity();
    foregroundLayer.attach(foreground);
    
    scene.attachChild(backgroundLayer);
    scene.attachChild(foregroundLayer);
    

    I hope this will help you.