Java 多层背景(如视差)
我试图为我的游戏创造一个背景,给人一种感觉,有些东西比其他东西更远。 假设我有天空,太阳,云和地面。天空和太阳是最远的,中间的云和地面在前面。 现在,如果玩家缩放或平移相机,我希望进一步的层移动得比较近的层少,以给人一种感觉,即它们确实更远(地面移动得最多,云层移动得较少,太阳几乎不移动) 就像自动视差的表现方式一样,只是它是一个静态背景,所以除非玩家放大或缩小,否则通常不会移动 这个概念是在愤怒的小鸟中用游戏对象(鸟,猪,块)来实现的,地面在前面,中间的一个山层和天空,云层是最远的。 现在,如果你明白我想要完成什么,请给我一个想法如何去做,因为这让我在过去的几天里发疯,我甚至不能把这个概念付诸实施。Java 多层背景(如视差),java,android,andengine,Java,Android,Andengine,我试图为我的游戏创造一个背景,给人一种感觉,有些东西比其他东西更远。 假设我有天空,太阳,云和地面。天空和太阳是最远的,中间的云和地面在前面。 现在,如果玩家缩放或平移相机,我希望进一步的层移动得比较近的层少,以给人一种感觉,即它们确实更远(地面移动得最多,云层移动得较少,太阳几乎不移动) 就像自动视差的表现方式一样,只是它是一个静态背景,所以除非玩家放大或缩小,否则通常不会移动 这个概念是在愤怒的小鸟中用游戏对象(鸟,猪,块)来实现的,地面在前面,中间的一个山层和天空,云层是最远的。 现在,如
任何代码都很好。您可以尝试使用Andengine来实现这样的目的:
另见: 实现这样的效果非常容易,下面是一个示例:
@Override
public void onLoadResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);
this.mEnemyTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "enemy.png", 73, 0, 3, 4);
this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_front.png", 0, 0);
this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_back.png", 0, 188);
this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_mid.png", 0, 669);
this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas, this.mAutoParallaxBackgroundTexture);
}
@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerBack.getHeight(), this.mParallaxLayerBack)));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f, new Sprite(0, 80, this.mParallaxLayerMid)));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerFront.getHeight(), this.mParallaxLayerFront)));
scene.setBackground(autoParallaxBackground);
/*
* Calculate the coordinates for the face, so its centered on the camera.
*/
final int playerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth()) / 2;
final int playerY = CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight() - 5;
/* Create two sprits and add it to the scene. */
final AnimatedSprite player = new AnimatedSprite(playerX, playerY, this.mPlayerTextureRegion);
player.setScaleCenterY(this.mPlayerTextureRegion.getTileHeight());
player.setScale(2);
player.animate(new long[] { 200, 200, 200 }, 3, 5, true);
final AnimatedSprite enemy = new AnimatedSprite(playerX - 80, playerY, this.mEnemyTextureRegion);
enemy.setScaleCenterY(this.mEnemyTextureRegion.getTileHeight());
enemy.setScale(2);
enemy.animate(new long[] { 200, 200, 200 }, 3, 5, true);
scene.attachChild(player);
scene.attachChild(enemy);
return scene;
}
感谢您的回复这是一个很好的视频,但如何做是我需要的。这是自动视差示例的代码,这不是我需要的,因为它会产生缩放和平移问题。另外,我的游戏不是侧滚游戏,它是一个静态游戏,就像愤怒的小鸟一样。我所需要的是缩放或平移时的效果,而不是侧滚。你仍然可以使用autoParallax示例作为基础。只需使用0初始化视差实体,在缩放时,您可以根据需要修改这些实体,以实现您想要的视差效果。