2012-08-28 38 views
0
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) { 
      final Body carBody = CityRacerActivity.this.mCarBody; 

      final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5); 
      carBody.setLinearVelocity(velocity); 
      Vector2Pool.recycle(velocity); 

      final float rotationInRad = (float)Math.atan2(-pValueX, pValueY); 
      carBody.setTransform(carBody.getWorldCenter(), rotationInRad); 

      CityRacerActivity.this.mCar.setRotation(MathUtils.radToDeg(rotationInRad)); 
     } 

因此,這部分工作。控制精靈不完全工作

OnControlChange以正確的方向移動精靈,但是當我放開控件時,它似乎每次都將車輛向上移動。

我在android上使用andengine。

我的代碼基於racergameactivity示例,但此示例本身似乎已經存在此錯誤。

回答

2
 public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) { 
      final Body carBody = CityRacerActivity.this.mCarBody; 

      final float rotationInRad = (float)Math.atan2(-pValueX, pValueY); 

      if ((pValueX == 0) && (pValueY == 0)) 
      { 
        //Don't turn the body/sprite of the car 


      }else 
      { 
        carBody.setTransform(carBody.getWorldCenter(), rotationInRad); 
        //turn the car body in the direction of movement 
        CityRacerActivity.this.mCar.setRotation(MathUtils.radToDeg(rotationInRad)); 
      } 

      //set the velocity 
      final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5); 
      carBody.setLinearVelocity(velocity); 
      Vector2Pool.recycle(velocity); 
     } 

這個工程就像我想要的,但我不知道這是我應該如何處理它。