Andengine的自動視差背景AutoParallaxBackground(背景移動)

(轉自:http://blog.csdn.net/xyz_fly/article/details/7466643)

這次,我們來談談背景background的一些類及用法。關於background的用法,AndEngineExamples中有單獨的例子分別介紹了,在這裏,我們也只是將例子稍加改造。

AndEngineExamples中第一個例子便是AutoParallaxBackgroundExample——自動視差背景。也就是有層次感的背影移動變化。比如人在場景中跑動,我們爲了襯托出人物是在跑動,就會例如增加幾朵雲,讓它向人物跑動的方向移動,這樣人們就會認爲人物是跑起來的。但實際上,人物並沒有移動位置。

早期,做過橫版過關遊戲的人都要自己去實現視差背景,大致上講,就是讓人物座標基本不動,讓背景也保持不動,讓中間一層(街道,地面)向人物相反方向移動。如果想做得更精細些,就會增加一些雲彩,但云彩又不能和地面移動的速度一樣,就需要單獨設置雲彩的移動速度。

好了,andengine已經爲我設計好了這些接下來看看它是怎麼做的。

AutoParallaxBackground——自動視差背景類,我們所有操作都要基於它的對象來實現。
AutoParallaxBackground只有一個構造方法:
public AutoParallaxBackground(final float pRed, final float pGreen, final float pBlue, final float pParallaxChangePerSecond) :
前三個很簡單,分別對應的顏色數值;pParallaxChangePerSecond爲背景每秒移動的距離。

讓我很不解的是:以AutoParallaxBackground作者的風格,會封裝很多適合大家所需的方法。背景顏色一般沒人會去設置,所以,這裏其實還應該再增加一個構造方法:

[java] view plaincopy
  1. public AutoParallaxBackground(final float pParallaxChangePerSecond) {  
  2.     super(000);  
  3.     this.mParallaxChangePerSecond = pParallaxChangePerSecond;  
  4. }  

public void attachParallaxEntity(final ParallaxEntity pParallaxEntity),這個方法來自於其父類,需要傳入一個ParallaxEntity的對象。
從ParallaxEntity的構造方法來看:
public ParallaxEntity(final float pParallaxFactor, final IAreaShape pAreaShape) :pParallaxFactor背景移動的相對數值。pAreaShape一般爲精靈對象。

public void setParallaxValue(final float pParallaxValue):設置背景的初始位置。你可以理解成X軸的位置。

public void setParallaxChangePerSecond(final float pParallaxChangePerSecond):設置每秒移動的距離,這個和構造方法中的最後一個參數是一樣的。我們主要就是通過改變這個值來實現背景的移動。

這裏直接用AutoParallaxBackgroundExample的例子,並稍加改造:
[java] view plaincopy
  1. public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  2.         throws Exception {  
  3.     Scene mScene = new Scene();  
  4.   
  5.     // 最後一個參數原來是5,我們這裏設爲0,意思是讓它一開始不要滾動  
  6.     final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(  
  7.             0000);  
  8.   
  9.     // 0.0f,-0.5f,-10.0f:你可以把它們理解爲相對位置差  
  10.     autoParallaxBackground  
  11.             .attachParallaxEntity(new ParallaxEntity(0.0f,  
  12.                     new Sprite(0, CAMERA_HEIGHT  
  13.                             - this.mParallaxLayerBack.getHeight(),  
  14.                             this.mParallaxLayerBack,  
  15.                             getVertexBufferObjectManager())));  
  16.     autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f,  
  17.             new Sprite(080this.mParallaxLayerMid,  
  18.                     getVertexBufferObjectManager())));  
  19.     autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f,  
  20.             new Sprite(0, CAMERA_HEIGHT  
  21.                     - this.mParallaxLayerFront.getHeight(),  
  22.                     this.mParallaxLayerFront,  
  23.                     getVertexBufferObjectManager())));  
  24.   
  25.     // 設置背景  
  26.     mScene.setBackground(autoParallaxBackground);  
  27.   
  28.     final int playerX = (int) (CAMERA_WIDTH - this.mPlayerTextureRegion  
  29.             .getWidth()) / 2;  
  30.     final int playerY = (int) (CAMERA_HEIGHT  
  31.             - this.mPlayerTextureRegion.getHeight() - 5);  
  32.     final AnimatedSprite player = new AnimatedSprite(playerX, playerY,  
  33.             this.mPlayerTextureRegion, getVertexBufferObjectManager());  
  34.     player.setScaleCenterY(this.mPlayerTextureRegion.getHeight());  
  35.     player.setScale(2);  
  36.     player.animate(new long[] { 200200200 }, 35true);  
  37.   
  38.     mScene.attachChild(player);  
  39.   
  40.     // 我們新增加一個觸摸事件監聽  
  41.     mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {  
  42.         public boolean onSceneTouchEvent(Scene pScene,  
  43.                 TouchEvent pSceneTouchEvent) {  
  44.             switch (pSceneTouchEvent.getAction()) {  
  45.             case TouchEvent.ACTION_UP:// 當觸屏擡起的時候  
  46.                 // 如果在右邊觸摸,我們讓屏幕向左滾動  
  47.                 if (pSceneTouchEvent.getX() > 400) {  
  48.                     // 設置每秒鐘背景滾動的距離  
  49.                     autoParallaxBackground.setParallaxChangePerSecond(10);  
  50.                     // 設置一下小人的幀序列  
  51.                     player.animate(new long[] { 200200200 }, 35true);  
  52.                 }  
  53.                 // 如果在左邊觸摸,我們讓屏幕向右滾動  
  54.                 else {  
  55.                     // 設置每秒鐘背景滾動的距離  
  56.                     autoParallaxBackground.setParallaxChangePerSecond(-10);  
  57.                     // 設置一下小人的幀序列  
  58.                     player.animate(new long[] { 200200200 }, 911,  
  59.                             true);  
  60.                 }  
  61.                 break;  
  62.             }  
  63.             return true;  
  64.         }  
  65.     });  
  66.     pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  67. }  


好了,你看明白了嗎,以下是源代碼:
[java] view plaincopy
  1. package com.testsprite;  
  2.   
  3. import org.andengine.engine.camera.Camera;  
  4. import org.andengine.engine.options.EngineOptions;  
  5. import org.andengine.engine.options.EngineOptions.ScreenOrientation;  
  6. import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;  
  7. import org.andengine.entity.modifier.LoopEntityModifier;  
  8. import org.andengine.entity.scene.IOnSceneTouchListener;  
  9. import org.andengine.entity.scene.Scene;  
  10. import org.andengine.entity.scene.background.AutoParallaxBackground;  
  11. import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;  
  12. import org.andengine.entity.sprite.AnimatedSprite;  
  13. import org.andengine.entity.sprite.Sprite;  
  14. import org.andengine.input.touch.TouchEvent;  
  15. import org.andengine.opengl.texture.TextureOptions;  
  16. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;  
  17. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;  
  18. import org.andengine.opengl.texture.region.TextureRegion;  
  19. import org.andengine.opengl.texture.region.TiledTextureRegion;  
  20. import org.andengine.ui.activity.BaseGameActivity;  
  21.   
  22. public class TestSprite extends BaseGameActivity {  
  23.     private static final int CAMERA_WIDTH = 720;  
  24.     private static final int CAMERA_HEIGHT = 480;  
  25.   
  26.     private BitmapTextureAtlas mBitmapTextureAtlas;  
  27.     private TiledTextureRegion mPlayerTextureRegion;  
  28.   
  29.     private BitmapTextureAtlas mAutoParallaxBackgroundTexture;  
  30.   
  31.     private TextureRegion mParallaxLayerBack;  
  32.     private TextureRegion mParallaxLayerMid;  
  33.     private TextureRegion mParallaxLayerFront;  
  34.   
  35.     public EngineOptions onCreateEngineOptions() {  
  36.         Camera mCamera = new Camera(00, CAMERA_WIDTH, CAMERA_HEIGHT);  
  37.         EngineOptions mEngineOptions = new EngineOptions(true,  
  38.                 ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),  
  39.                 mCamera);  
  40.         return mEngineOptions;  
  41.     }  
  42.   
  43.     public void onCreateResources(  
  44.             OnCreateResourcesCallback pOnCreateResourcesCallback)  
  45.             throws Exception {  
  46.         this.mBitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(),  
  47.                 128128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);  
  48.         this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory  
  49.                 .createTiledFromAsset(this.mBitmapTextureAtlas, this,  
  50.                         "player.png"0034);  
  51.   
  52.         this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(  
  53.                 getTextureManager(), 10241024, TextureOptions.DEFAULT);  
  54.         this.mParallaxLayerFront = (TextureRegion) BitmapTextureAtlasTextureRegionFactory  
  55.                 .createFromAsset(this.mAutoParallaxBackgroundTexture, this,  
  56.                         "parallax_background_layer_front.png"00);  
  57.         this.mParallaxLayerBack = (TextureRegion) BitmapTextureAtlasTextureRegionFactory  
  58.                 .createFromAsset(this.mAutoParallaxBackgroundTexture, this,  
  59.                         "parallax_background_layer_back.png"0188);  
  60.         this.mParallaxLayerMid = (TextureRegion) BitmapTextureAtlasTextureRegionFactory  
  61.                 .createFromAsset(this.mAutoParallaxBackgroundTexture, this,  
  62.                         "parallax_background_layer_mid.png"0669);  
  63.   
  64.         mBitmapTextureAtlas.load();  
  65.         mAutoParallaxBackgroundTexture.load();  
  66.   
  67.         pOnCreateResourcesCallback.onCreateResourcesFinished();  
  68.     }  
  69.   
  70.     LoopEntityModifier mLoopEntityModifier;  
  71.   
  72.     public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  73.             throws Exception {  
  74.         Scene mScene = new Scene();  
  75.   
  76.         // 最後一個參數原來是5,我們這裏設爲0,意思是讓它一開始不要滾動  
  77.         final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(  
  78.                 0000);  
  79.   
  80.         // 0.0f,-0.5f,-10.0f:你可以把它們理解爲相對位置差  
  81.         autoParallaxBackground  
  82.                 .attachParallaxEntity(new ParallaxEntity(0.0f,  
  83.                         new Sprite(0, CAMERA_HEIGHT  
  84.                                 - this.mParallaxLayerBack.getHeight(),  
  85.                                 this.mParallaxLayerBack,  
  86.                                 getVertexBufferObjectManager())));  
  87.         autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f,  
  88.                 new Sprite(080this.mParallaxLayerMid,  
  89.                         getVertexBufferObjectManager())));  
  90.         autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f,  
  91.                 new Sprite(0, CAMERA_HEIGHT  
  92.                         - this.mParallaxLayerFront.getHeight(),  
  93.                         this.mParallaxLayerFront,  
  94.                         getVertexBufferObjectManager())));  
  95.   
  96.         // 設置背景  
  97.         mScene.setBackground(autoParallaxBackground);  
  98.   
  99.         final int playerX = (int) (CAMERA_WIDTH - this.mPlayerTextureRegion  
  100.                 .getWidth()) / 2;  
  101.         final int playerY = (int) (CAMERA_HEIGHT  
  102.                 - this.mPlayerTextureRegion.getHeight() - 5);  
  103.         final AnimatedSprite player = new AnimatedSprite(playerX, playerY,  
  104.                 this.mPlayerTextureRegion, getVertexBufferObjectManager());  
  105.         player.setScaleCenterY(this.mPlayerTextureRegion.getHeight());  
  106.         player.setScale(2);  
  107.         player.animate(new long[] { 200200200 }, 35true);  
  108.   
  109.         mScene.attachChild(player);  
  110.   
  111.         // 我們新增加一個觸摸事件監聽  
  112.         mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {  
  113.             public boolean onSceneTouchEvent(Scene pScene,  
  114.                     TouchEvent pSceneTouchEvent) {  
  115.                 switch (pSceneTouchEvent.getAction()) {  
  116.                 case TouchEvent.ACTION_UP:// 當觸屏擡起的時候  
  117.                     // 如果在右邊觸摸,我們讓屏幕向左滾動  
  118.                     if (pSceneTouchEvent.getX() > 400) {  
  119.                         // 設置每秒鐘背景滾動的距離  
  120.                         autoParallaxBackground.setParallaxChangePerSecond(10);  
  121.                         // 設置一下小人的幀序列  
  122.                         player.animate(new long[] { 200200200 }, 35true);  
  123.                     }  
  124.                     // 如果在左邊觸摸,我們讓屏幕向右滾動  
  125.                     else {  
  126.                         // 設置每秒鐘背景滾動的距離  
  127.                         autoParallaxBackground.setParallaxChangePerSecond(-10);  
  128.                         // 設置一下小人的幀序列  
  129.                         player.animate(new long[] { 200200200 }, 911,  
  130.                                 true);  
  131.                     }  
  132.                     break;  
  133.                 }  
  134.                 return true;  
  135.             }  
  136.         });  
  137.         pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  138.     }  
  139.   
  140.     public void onPopulateScene(Scene pScene,  
  141.             OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {  
  142.         pOnPopulateSceneCallback.onPopulateSceneFinished();  
  143.     }  
  144. }  


自動視差背景

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章