0
\$\begingroup\$

I'm new to game development with AndEngine. I want to add ParallaxBackground but I don't know how to change the background when the player moves. I'm using arrows for moving a player.

Now my question is where I write the code parallaxBackground.setParallaxValue(5);?

I have written this line in onAreaTouched method of arrow but it does not work.

Code

private Camera mCamera;
private static  int CAMERA_WIDTH = 800;
private static  int CAMERA_HEIGHT = 480;

private BitmapTextureAtlas bgTexture;
private ITextureRegion bgTextureRegion;

@Override
protected void onCreateResources() {

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    bgTexture = new BitmapTextureAtlas(getTextureManager(),2160,480,TextureOptions.REPEATING_BILINEAR);
    bgTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bgTexture, this, "background.png", 0, 0);
    bgTexture.load(); 
}
@Override
protected Scene onCreateScene() {

    this.getEngine().registerUpdateHandler(new FPSLogger());

    Scene scene = new Scene();
    scene.setBackground(new Background(Color.BLACK));

    final ParallaxBackground parallaxBackground = new ParallaxBackground(0, 0, 0);
    final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();

    parallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - this.bgTextureRegion.getHeight(), this.bgTextureRegion, vertexBufferObjectManager)));
    scene.setBackground(parallaxBackground);

Robot robot = new Robot();

    // add Player
    final AnimatedSprite animatedRobotSprite = new AnimatedSprite(robot.centerX, robot.centerY, 122, 126, (ITiledTextureRegion) robotTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(animatedRobotSprite);
    animatedRobotSprite.animate(new long[]{1250,50,50});


    // add right arrow button
    Sprite rightArrowSprite = new Sprite(0, CAMERA_HEIGHT-70, rightArrowTextureRegion, getVertexBufferObjectManager()){

        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY) {

            switch (pSceneTouchEvent.getAction()) {

            case TouchEvent.ACTION_DOWN:
                moveRight = true;
                parallaxBackground.setParallaxValue(5);
                break;

            case TouchEvent.ACTION_MOVE:
                moveRight = true;
                break;

            case TouchEvent.ACTION_UP:
                moveRight = false;
                break;

            default:
                break;
            }
            return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
        }
    };
    scene.attachChild(rightArrowSprite);

    scene.registerTouchArea(rightArrowSprite);

    scene.setTouchAreaBindingOnActionDownEnabled(true);

    scene.setTouchAreaBindingOnActionMoveEnabled(true);

    scene.registerUpdateHandler(new IUpdateHandler() {

        @Override
        public void reset() {

        }

        @Override
        public void onUpdate(float pSecondsElapsed) {

            if ( moveRight ){
                animatedRobotSprite.setPosition(animatedRobotSprite.getX()+speedX, animatedRobotSprite.getY());


            }
        }
    });

    return scene;
}
\$\endgroup\$
2
\$\begingroup\$

The Parallax layers themselves can be quite a bit of trouble- especially if you want them to move with the character rather than just being static or moving at a constant rate of speed.

My suggestion: One AndEngine user uploaded a custom ParallaxLayer if you're in the GLES2.0 branch that can be found here, I'd recommend using it as it supports scrolling at variable rates and overall seems like more of what you're looking for. Good luck with AndEngine- it's a fun way to make an Android game!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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