简体   繁体   English

视差背景和引擎

[英]ParallaxBackground AndEngine

I'm new to game development andengine. 我是游戏开发和引擎的新手。 I want to add ParallaxBackground but I don't know how to change background on player move. 我想添加ParallaxBackground,但是我不知道如何在玩家移动时更改背景。 I'm using arrow for moving a player. 我正在使用箭头移动玩家。 Now my question is where I write the code parallaxBackground.setParallaxValue(5); 现在我的问题是我在哪里编写代码parallaxBackground.setParallaxValue(5); I was written this line in onAreaTouched method of arrow but it not work. 我在arrow的onAreaTouched方法中编写了此行,但它不起作用。 please help me. 请帮我。 Thanks. 谢谢。

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;
}

I see at least on possible problem: You have only one ParallaxEntity attached. 我至少看到一个可能的问题:您仅附加了一个ParallaxEntity。 The visual effect of parallax is created by multiple entities moving at different speeds. 视差的视觉效果是由多个以不同速度移动的实体创建的。 But I think what you are seeing is that your background is not scrolling. 但是我认为您看到的是您的背景没有滚动。 If you do not use the AutoParallaxBackground class, you have to update the paralax amount on each update. 如果不使用AutoParallaxBackground类,则必须在每次更新时更新视差量。

Inside the autoparallax class, this is what it does onUpdate(): 在autoparallax类中,这是onUpdate()的作用:

@Override
    public void onUpdate(final float pSecondsElapsed) {
        super.onUpdate(pSecondsElapsed);

        this.mParallaxValue += this.mParallaxChangePerSecond * pSecondsElapsed;
    }

Looking at this you can see that the setParallaxValue() is an absolute number, so to make it continually scroll, you will need to feed it a new number on each update. 查看此内容,您可以看到setParallaxValue()是绝对数字,因此要使其不断滚动,您需要在每次更新时为其提供新的数字。 For example, in you main game update loop, you could feed in your player.getX() into parallaxBackground.setParallaxValue() like so: 例如,在主游戏更新循环中,您可以将player.getX()馈入parallaxBackground.setParallaxValue(),如下所示:

parallaxBackground.setParallaxValue(rightArrowSprite.getX());

While this may not be the exact effect you want, you should now see the background moving when your character moves. 尽管这可能并不是您想要的确切效果,但是现在您应该看到角色移动时背景正在移动。

There are different modifiers foe updating your entity(AnimatedSprite) you can use them for movement like 有不同的修饰符可以更新您的实体(AnimatedSprite),您可以将它们用于运动,例如

final Entity playerEntity = ...;//Get player entity here.

final float jumpDuration = 2;
final float x= playerEntity.getX();



final MoveXModifiermoveModifier = new MoveXModifier(jumpDuration / 2, x+ 100, y);


playerEntity.registerEntityModifier(moveModifier );

It's not necessary to have two background moving at different speeds. 不必让两个背景以不同的速度移动。 With one fixed background and one "moving" background you will notice the effect when the player moves. 使用一个固定的背景和一个“移动”的背景,您会注意到玩家移动时的效果。 However, I've tried setting an absolute value in 但是,我尝试在

parallaxBackground.setParallaxValue(10f);

And the effect was not continuous. 而且效果不是连续的。 What it worked for me was a recipe found in Android Cookbook which is based on overriding the onUpdate of the background to change only when the camera has moved. 它对我有用的是在Android Cookbook中找到的食谱,该食谱基于将背景的onUpdate覆盖为仅在相机移动时才更改。

 ParallaxBackground background = new ParallaxBackground(0.3f, 0.3f,0.9f) {
 /* We'll use these values to calculate the parallax value of the background*/

 float cameraPreviousX = 0;
 float parallaxValueOffset = 0;

 /* onUpdates to the background, we need to calculate new
 * parallax values in order to apply movement to the background
 * objects (the hills in this case) */

  @Override

public void onUpdate(float pSecondsElapsed) {

 /* Obtain the camera's current center X value */
 final float cameraCurrentX = mCamera.getCenterX();

 /* If the camera's position has changed since last update... */
 if (cameraPreviousX != cameraCurrentX) {

 /* Calculate the new parallax value offset by subtracting the previous update's camera x coordinate from the current update's camera x coordinate */
  parallaxValueOffset += cameraCurrentX - cameraPreviousX;

  /* Apply the parallax value offset to the background, which will in-turn offset the positions of entities attached to the background */
  this.setParallaxValue(parallaxValueOffset);

  /* Update the previous camera X since we're finished with this update */
  cameraPreviousX = cameraCurrentX;

 }
 super.onUpdate(pSecondsElapsed);

 }
};

background.attachParallaxEntity(new ParallaxEntity(5, hillFurthest));

Note than depending on the camera configuration, the "5" value included in the "attachParallaxEntity" may need to be negative. 请注意,取决于摄像机配置,“ attachParallaxEntity”中包含的“ 5”值可能需要为负。 Otherwise, the background will move in the opposite direction of the player. 否则,背景将以与播放器相反的方向移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM