AndEngine中移动背景(上下移动、斜着移动)

在AndEngine中移动背景以使得道具/人物在屏幕地图上移动,类似赛车、滑雪等游戏看着像人物在移动,其实动的是背景,所以移动背景就可以模拟出在屏幕上的移动

在AndEngineExamples中有一个移动背景的例子:AutoParallaxBackgroundExample,在该类中使用了AutoParallaxBackground作为自循环绘画的背景在使用

但该例子中只有横向背景的移动,竖向方向的移动没有做,看AndEngine中的代码也没有关于竖向方向的计算、绘图,因此扩充了ParallaxBackground中的ParallaxEntity实现以便于移动背景的横向、竖向、斜向的移动,代码见下面:替换掉原版的后重新生成andengine.jar,然后放到自己的工程下面即可

public static class ParallaxEntity {
		// ===========================================================
		// Constants
		// ===========================================================

		// ===========================================================
		// Fields
		// ===========================================================

		float mParallaxFactorX;
		float mParallaxFactorY;
		final IAreaShape mAreaShape;

		// ===========================================================
		// Constructors
		// ===========================================================

		public ParallaxEntity(final float pParallaxFactorX, final IAreaShape pAreaShape) {
			this.mParallaxFactorX = pParallaxFactorX;
			this.mParallaxFactorY = 0;
			this.mAreaShape = pAreaShape;
		}

		public ParallaxEntity(final float pParallaxFactorX, final float pParallaxFactorY, final IAreaShape pAreaShape) {
			this.mParallaxFactorX = pParallaxFactorX;
			this.mParallaxFactorY = pParallaxFactorY;
			this.mAreaShape = pAreaShape;
		}

		// ===========================================================
		// Getter & Setter
		// ===========================================================

		// ===========================================================
		// Methods for/from SuperClass/Interfaces
		// ===========================================================

		// ===========================================================
		// Methods
		// ===========================================================

		public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValue) {
			pGLState.pushModelViewGLMatrix();
			{
				final float cameraWidth = mParallaxFactorX != 0 ? pCamera.getWidth() : 0;
				final float cameraHeight = mParallaxFactorY != 0 ? pCamera.getHeight() : 0;
				final float shapeWidthScaled = this.mAreaShape.getWidthScaled();
				final float shapeHeightScaled = this.mAreaShape.getHeightScaled();
				
				float baseOffsetX = mParallaxFactorX != 0 ? (pParallaxValue * this.mParallaxFactorX) % shapeWidthScaled : 0;
				float baseOffsetY = mParallaxFactorY != 0 ? (pParallaxValue * this.mParallaxFactorY) % shapeHeightScaled : 0;

				//
				while(baseOffsetX > 0) {
					baseOffsetX -= shapeWidthScaled;
				}
				while(baseOffsetY > 0) {
					baseOffsetY -= shapeHeightScaled;
				}
				pGLState.translateModelViewGLMatrixf(baseOffsetX, baseOffsetY, 0);

				//
				float currentMaxX = baseOffsetX;
				float currentMaxY = baseOffsetY;
				boolean	bX = mParallaxFactorX != 0 ? true : false;
				boolean	bY = mParallaxFactorY != 0 ? true : false;
				do {
					this.mAreaShape.onDraw(pGLState, pCamera);
					
					if (bX && bY){
						pGLState.translateModelViewGLMatrixf(shapeWidthScaled, shapeHeightScaled, 0);
						currentMaxX += shapeWidthScaled;
						currentMaxY += shapeHeightScaled;
					}
					else if (bX && !bY){
						pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
						currentMaxX += shapeWidthScaled;
					}
					else if (!bX && bY){
						pGLState.translateModelViewGLMatrixf(0, shapeHeightScaled, 0);
						currentMaxY += shapeHeightScaled;
					}
					
					bX = currentMaxX < cameraWidth ? true : false;
					bY = currentMaxY < cameraHeight ? true : false;
				} while(bX || bY);
			}
			pGLState.popModelViewGLMatrix();
		}

		// ===========================================================
		// Inner and Anonymous Classes
		// ===========================================================
	}

//位于ParallaxBackground类中


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值