2015-11-08 46 views
0

我正在開發一個遊戲AndEngine,我想附加一個精靈到視差背景(在我的主菜單上),但我不想讓精靈重複(這是目前正在發生的事情)。Andengine - 將精靈附加到視差背景仍然導致它重複

我已經嘗試了這個(下面)的工作,但我在遊戲中使用的精靈,所以當我回到主菜單,精靈將移動(我試圖重置精靈,但似乎沒有工作)。

Sprite playerCar = new Sprite(playerX, playerY, 
      mResourceManager.mPlayerCarTextureRegion, 
      mVertexBufferObjectManager); 
    playerCar.setRotation(-15); 
    attachChild(playerCar); 

我想要做的是以下幾點:

定義我的精靈正常:

Sprite playerCar = new Sprite(playerX, playerY, 
      mResourceManager.mPlayerCarTextureRegion, 
      mVertexBufferObjectManager); 
    playerCar.setRotation(-15); 

然後將其連接到我的背景:

ParallaxBackground menuParallaxBackground = new ParallaxBackground(0, 
      0, 0); 

    menuParallaxBackground.attachParallaxEntity(new ParallaxEntity(0, 
      new Sprite(0, SCREEN_HEIGHT 
        - mResourceManager.mParallaxLayerRoad.getHeight(), 
        mResourceManager.mParallaxLayerRoad, 
        mVertexBufferObjectManager))); 

    menuParallaxBackground.attachParallaxEntity(new ParallaxEntity(0, 
      playerCar)); 

也可以工作但汽車不斷重複我不想要的。

任何幫助,將不勝感激!謝謝。

+1

看看視差類的onManagedDraw方法,你會明白爲什麼連接到ParallaxEntity的每個實體都重複了! – sjkm

+0

你是否試圖簡單地通過menuParallaxBackground.attachChild()將它附加到背景?沒有檢查這是否可能,只是一個想法... –

+0

@sjkm啊是啊在while循環onDraw --' while(currentMaxX

回答

0

問題是因爲ParallaxBackground類中ParallaxEntity類的onDraw方法。在精靈繪製的地方存在一個循環,直到精靈填滿屏幕寬度爲止。我只是創建了一個自定義類,並刪除了while循環,所以我可以將它用於我的主菜單。

ParallaxEntity的onDraw代碼之前:

public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValue) { 
     pGLState.pushModelViewGLMatrix(); 
     { 
      final float cameraWidth = pCamera.getWidth(); 
      final float shapeWidthScaled = this.mAreaShape.getWidthScaled(); 
      float baseOffset = (pParallaxValue * this.mParallaxFactor) % shapeWidthScaled; 

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

      float currentMaxX = baseOffset; 

      do { 
       this.mAreaShape.onDraw(pGLState, pCamera); 
       pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0); 
       currentMaxX += shapeWidthScaled; 
      } while(currentMaxX < cameraWidth); 
     } 
     pGLState.popModelViewGLMatrix(); 
    } 

後我CustomParallaxEntity的onDraw代碼(注意最後while循環刪除):

public void onDraw(final GLState pGLState, final Camera pCamera, 
      final float pParallaxValue) { 
     pGLState.pushModelViewGLMatrix(); 
     { 
      final float shapeWidthScaled = this.mAreaShape.getWidthScaled(); 
      float baseOffset = (pParallaxValue * this.mParallaxFactor) 
        % shapeWidthScaled; 

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

      this.mAreaShape.onDraw(pGLState, pCamera); 
      pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0); 
     } 
     pGLState.popModelViewGLMatrix(); 
    } 

感謝幫助我找出一個對我的問題的意見解。