gpt4 book ai didi

android - 视差背景和引擎

转载 作者:行者123 更新时间:2023-11-29 21:37:27 25 4
gpt4 key购买 nike

我是游戏开发和引擎的新手。我想添加 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;
}

最佳答案

我至少看到了一个可能的问题:您只附加了一个 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());

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

关于android - 视差背景和引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18075263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com