2014-03-26 28 views

回答

0

做到這一點的一種方法是擴展ParallaxBackground類中包含的ParallaxEntity並使其垂直移動,這裏有一些代碼展示了我如何做到這一點。它基本上將x座標的變化交換爲y座標。我已將它改編成當前版本的AndEngine,原始版本的代碼可以找到舊版本的代碼here

import org.andengine.engine.camera.Camera; 
import org.andengine.entity.IEntity; 
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity; 
import org.andengine.opengl.util.GLState; 

public class VerticalParallaxEntity extends ParallaxEntity 
{ 
    final float mParallaxFactor; 
    final IEntity mEntity; 

    public VerticalParallaxEntity(final float pParallaxFactor, final IEntity pEntity) 
    { 
     super(pParallaxFactor, pEntity); 
     this.mParallaxFactor = pParallaxFactor; 
     this.mEntity = pEntity; 
    } 

    public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValue) 
    { 
     pGLState.pushModelViewGLMatrix(); 
     { 
      final float cameraHeight = pCamera.getHeight(); 
      final float entityHeightScaled = this.mEntity.getHeight() * this.mEntity.getScaleY(); 

      float baseOffset = (pParallaxValue * this.mParallaxFactor) % entityHeightScaled; 

      while(baseOffset > 0) { 
       baseOffset -= entityHeightScaled; 
      } 
      pGLState.translateModelViewGLMatrixf(0, baseOffset, 0); 

      float currentMaxY = baseOffset; 

      do 
      { 
       this.mEntity.onDraw(pGLState, pCamera); 
       pGLState.translateModelViewGLMatrixf(0, entityHeightScaled, 0); 
       currentMaxY += entityHeightScaled; 
      } while(currentMaxY < cameraHeight); 
     } 
     pGLState.popModelViewGLMatrix(); 
    } 
}