2012-05-29 54 views
1

我想爲我的遊戲創建一個背景,給人的感覺是有些東西比其他東西更深。所以可以說我有一個天空,一個太陽,一些雲彩和地面。天空和太陽是最遠的,中間的雲層和地面在前面。 現在,如果玩家放大或平移相機,我希望進一步的圖層比較近的圖層移動的更少,以給人留下更深的印象(地面移動最多,雲移動更少,太陽幾乎不移動) 就像汽車視差行爲的方式除了它是一個靜態背景,所以通常沒有任何動作,除非玩家放大或縮小。與幾層(如視差)的背景

這個概念是用憤怒的小鳥來實現的,遊戲中的物體(鳥,豬,塊......)和地面都在前面,中間是一個山層,天空和雲層是最遠的。

現在,如果你明白我在做什麼,請給我一個想法如何去做這件事,因爲它讓我瘋狂的過去幾天,我甚至不能把這個概念實現它呢。任何代碼也會很好。

回答

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; 
    } 
+0

謝謝你的迴應,它是一個很好的視頻,但如何做到這一點是我需要的。 – Ayham

+0

順便說一句,我已經使用andengine – Ayham

+0

編輯過的文章;-) – Thkru