AndEngineExamples中第一个例子便是AutoParallaxBackgroundExample——自动视差背景。也就是有层次感的背影移动变化。比如人在场景中跑动,我们为了衬托出人物是在跑动,就会例如增加几朵云,让它向人物跑动的方向移动,这样人们就会认为人物是跑起来的。但实际上,人物并没有移动位置。
早期,做过横版过关游戏的人都要自己去实现视差背景,大致上讲,就是让人物坐标基本不动,让背景也保持不动,让中间一层(街道,地面)向人物相反方向移动。如果想做得更精细些,就会增加一些云彩,但云彩又不能和地面移动的速度一样,就需要单独设置云彩的移动速度。
好了,andengine已经为我设计好了这些接下来看看它是怎么做的。
AutoParallaxBackground——自动视差背景类,我们所有操作都要基于它的对象来实现。
AutoParallaxBackground只有一个构造方法:
public AutoParallaxBackground(final float pRed, final float pGreen, final float pBlue, final float pParallaxChangePerSecond) :
前三个很简单,分别对应的颜色数值;pParallaxChangePerSecond为背景每秒移动的距离。
让我很不解的是:以AutoParallaxBackground作者的风格,会封装很多适合大家所需的方法。背景颜色一般没人会去设置,所以,这里其实还应该再增加一个构造方法:
- public AutoParallaxBackground(final float pParallaxChangePerSecond) {
- super(0, 0, 0);
- this.mParallaxChangePerSecond = pParallaxChangePerSecond;
- }
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的例子,并稍加改造:
- public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
- throws Exception {
- Scene mScene = new Scene();
- // 最后一个参数原来是5,我们这里设为0,意思是让它一开始不要滚动
- final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(
- 0, 0, 0, 0);
- // 0.0f,-0.5f,-10.0f:你可以把它们理解为相对位置差
- autoParallaxBackground
- .attachParallaxEntity(new ParallaxEntity(0.0f,
- new Sprite(0, CAMERA_HEIGHT
- - this.mParallaxLayerBack.getHeight(),
- this.mParallaxLayerBack,
- getVertexBufferObjectManager())));
- autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f,
- new Sprite(0, 80, this.mParallaxLayerMid,
- getVertexBufferObjectManager())));
- autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f,
- new Sprite(0, CAMERA_HEIGHT
- - this.mParallaxLayerFront.getHeight(),
- this.mParallaxLayerFront,
- getVertexBufferObjectManager())));
- // 设置背景
- mScene.setBackground(autoParallaxBackground);
- final int playerX = (int) (CAMERA_WIDTH - this.mPlayerTextureRegion
- .getWidth()) / 2;
- final int playerY = (int) (CAMERA_HEIGHT
- - this.mPlayerTextureRegion.getHeight() - 5);
- final AnimatedSprite player = new AnimatedSprite(playerX, playerY,
- this.mPlayerTextureRegion, getVertexBufferObjectManager());
- player.setScaleCenterY(this.mPlayerTextureRegion.getHeight());
- player.setScale(2);
- player.animate(new long[] { 200, 200, 200 }, 3, 5, true);
- mScene.attachChild(player);
- // 我们新增加一个触摸事件监听
- mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {
- public boolean onSceneTouchEvent(Scene pScene,
- TouchEvent pSceneTouchEvent) {
- switch (pSceneTouchEvent.getAction()) {
- case TouchEvent.ACTION_UP:// 当触屏抬起的时候
- // 如果在右边触摸,我们让屏幕向左滚动
- if (pSceneTouchEvent.getX() > 400) {
- // 设置每秒钟背景滚动的距离
- autoParallaxBackground.setParallaxChangePerSecond(10);
- // 设置一下小人的帧序列
- player.animate(new long[] { 200, 200, 200 }, 3, 5, true);
- }
- // 如果在左边触摸,我们让屏幕向右滚动
- else {
- // 设置每秒钟背景滚动的距离
- autoParallaxBackground.setParallaxChangePerSecond(-10);
- // 设置一下小人的帧序列
- player.animate(new long[] { 200, 200, 200 }, 9, 11,
- true);
- }
- break;
- }
- return true;
- }
- });
- pOnCreateSceneCallback.onCreateSceneFinished(mScene);
- }