Andengine的自动视差背景AutoParallaxBackground(背景移动)

来源:互联网 发布:qq群关系数据库 种子 编辑:程序博客网 时间:2024/06/05 15:15

(转自: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. }  


自动视差背景

0 0
原创粉丝点击