java - 多层背景(如视差)

标签 java android andengine

我正在尝试为我的游戏创建一个背景,给人以某些东西比其他东西更远的印象。 所以假设我有天空、太阳、一些云和地面。天和日最远,云在中间,地在前。 现在,如果玩家缩放或平移相机,我希望更远的层比更近的层移动得更少,给人的印象是它们真的更远(地面移动最多,云移动得更少,太阳几乎不动) 就像自动视差的行为方式一样,只是它是静态背景,所以通常没有任何移动,除非玩家放大或缩小。

这个概念在带有游戏对象(小鸟、 pig 、方 block ...)的愤怒的小鸟中实现,地面在前面,山层在中间,天空和云彩在最远。

现在,如果您了解我正在努力完成的事情,请告诉我如何着手去做,因为过去几天它让我发疯,我什至无法将这个概念付诸实现。 任何代码也很棒。

最佳答案

您可以尝试使用 Andengine 来达到这样的目的:
http://www.youtube.com/watch?v=ug5vfys6MIA

另见:http://www.andengine.org/forums/features/parallaxlayer-t5390.html

实现这样的效果真的很容易,下面是一个例子:

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

关于java - 多层背景(如视差),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10798059/

相关文章:

android - 是否可以将计算机的处理能力共享给 Android 设备?

java - 如何使用 Spring 异常处理 POST REST 正确的错误状态代码

android - Android 中固定选项卡内的固定选项卡

java - 如何从任何文本中识别字体类型

android - 电源按钮中断时无法暂停声音

android - 单词搜索游戏问题

java - 我如何以编程方式同时将超过 1 种样式应用于单个节点

java - 运行后JFrame显示不正确

java - java中如何将for循环每次迭代的结果存储到数组中

java - Sprite 旋转的延迟