0

我是游戏开发和引擎的新手。我想添加 ParallaxBackground 但我不知道如何更改玩家移动的背景。我正在使用箭头来移动玩家。现在我的问题是我在哪里编写代码,parallaxBackground.setParallaxValue(5);我用箭头方法写了这一行,onAreaTouched但它不起作用。请帮我。谢谢。

代码

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

3 回答 3

1

有不同的修改器更新你的实体(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 );
于 2014-01-09T09:57:22.990 回答
1

我至少看到了可能的问题:您只有一个 ParallaxEntity 附加。视差的视觉效果是由多个以不同速度移动的实体创建的。但我认为你看到的是你的背景没有滚动。如果不使用 AutoParallaxBackground 类,则必须在每次更新时更新视差量。

在 autoparallax 类中,onUpdate() 是这样做的:

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

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

看这个你可以看到 setParallaxValue() 是一个绝对数字,所以要让它不断滚动,你需要在每次更新时给它一个新的数字。例如,在您的主游戏更新循环中,您可以将 player.getX() 输入 parallaxBackground.setParallaxValue() ,如下所示:

parallaxBackground.setParallaxValue(rightArrowSprite.getX());

虽然这可能不是您想要的确切效果,但您现在应该看到背景在您的角色移动时移动。

于 2013-08-07T17:45:50.420 回答
0

没有必要让两个背景以不同的速度移动。使用一种固定背景和一种“移动”背景,您会注意到玩家移动时的效果。但是,我尝试在

parallaxBackground.setParallaxValue(10f);

而且效果不连续。它对我有用的是在 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));

请注意,根据相机配置,“attachParallaxEntity”中包含的“5”值可能需要为负数。否则,背景将向玩家相反的方向移动。

于 2015-05-13T08:10:42.203 回答