2015-11-08 57 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(); 
    } 

感谢帮助我找出一个对我的问题的意见解。