andengine编程之background(一)

来源:互联网 发布:uart 单片机 编辑:程序博客网 时间:2024/05/19 00:36
这次,我们来谈谈背景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. }  
0 0
原创粉丝点击