几层背景(如视差)

时间:2012-05-29 11:16:45

标签: java android andengine

我正在尝试为我的游戏创建一个背景,给人的印象是某些事情比其他事情更进一步。 所以我要说我有天空,太阳,一些云和地面。天空和太阳是最远的,中间的云和地面在前面。 现在,如果玩家放大或平移相机,我希望更多的层移动得比更近的层更少,给人的印象是它们真的更远(地面移动最多,云层移动得更少,太阳几乎没有移动)移动) 就像自动视差的表现方式一样,除了它是一个静态的背景,所以除非玩家放大或缩小,否则通常什么都不会移动。

这个概念是在愤怒的小鸟中用游戏物体(鸟类,猪,块......)实现的,地面在前面,中间是山地层,天空和云层是最远的。 / p>

现在,如果你理解我想要完成的事情,请告诉我如何去做,因为它让我疯狂过去几天,我甚至无法将这个概念用于实现它。 任何代码都会很棒。

1 个答案:

答案 0 :(得分:9)

您可以尝试将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;
    }