Andengine 背景自动移动AutoParallaxBackground 1
在Andengine 用AutoParallaxBackground实现这个效果。
new AutoParallaxBackground(final float pRed, final float pGreen,final float pBlue, 背景颜色的三个值 红绿篮
final float pParallaxChangePerSecond) 图片移动的速度
AutoParallaxBackground(float pRed, float pGreen, float pBlue, float pParallaxChangePerSecond)
ParallaxBackground.attachParallaxEntity(ParallaxEntity pParallaxEntity)往视差背景对象中加入一个背景实体
ParallaxEntity(float pParallaxFactor, Shape pShape)构建一个背景实体,第一个参数表示视差移动的因数,为负表示相对前景
或英雄反向移动。绝对值越大移动的越快,也就说这个因数是靠前的背景比后面的背景的绝对值大。
private BitmapTextureAtlas mAutoParallaxBackgroundTexture; public ITextureRegion mParallaxLayerBack; private ITextureRegion mParallaxLayerMid; private ITextureRegion mParallaxLayerFront; private void initTexture() { this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas( mContext.getTextureManager(), 1024, 1024); this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory .createFromAsset(this.mAutoParallaxBackgroundTexture, mContext, "parallax_background_layer_front.png", 0, 0); this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory .createFromAsset(this.mAutoParallaxBackgroundTexture, mContext, "parallax_background_layer_back.png", 0, 188); this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory .createFromAsset(this.mAutoParallaxBackgroundTexture, mContext, "parallax_background_layer_mid.png", 0, 669); this.mAutoParallaxBackgroundTexture.load();
game_scene=initScene(); } private Scene initScene() { Scene pScene = new Scene(); mContext.getEngine().registerUpdateHandler(new FPSLogger()); final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground( 0, 0, 0, 5); final VertexBufferObjectManager vertexBufferObjectManager = mContext .getVertexBufferObjectManager(); autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, BaseData.CAMERA_HEIGHT - this.mParallaxLayerBack.getHeight(), this.mParallaxLayerBack, vertexBufferObjectManager))); autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f, new Sprite(0, 80, this.mParallaxLayerMid, vertexBufferObjectManager))); autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, BaseData.CAMERA_HEIGHT - this.mParallaxLayerFront.getHeight(), this.mParallaxLayerFront, vertexBufferObjectManager))); // 将这个背景设为场景的背 pScene.setBackground(autoParallaxBackground); return pScene; }