2012-02-04 90 views
0

我有一個名爲玩家的動畫精靈,我改變了動畫和使用onscreencontrol的方向。動畫似乎卡住和/或重複第一幀,而沒有經歷完整的動畫週期。我懷疑屏幕控制更新處理程序是否有時間完成所有步驟之前重置動畫?我不知道什麼是錯在這裏,這似乎像它應該工作:Android AndEngine動畫精靈沒有正確動畫

final AnalogOnScreenControl velocityOnScreenControl = new AnalogOnScreenControl(x1, y1, this.BoundChaseCamera,this.ScreenControlBaseTextureRegion,this.ScreenControlKnobTextureRegion, 0.1f,new IAnalogOnScreenControlListener() { 
    @Override 
    public void onControlChange(
    final BaseOnScreenControl pBaseOnScreenControl, 
    final float pValueX, final float pValueY) { 
    /* player position */ 
    float spotX = player.getX() + 1 * TILE_WIDTH * pValueX; 
    float spotY = player.getY() + 1 * TILE_HEIGHT * pValueY; 
    /* if player position within bounds of map */ 
     if (spotX < TMXMapLayer.getWidth() 
     && spotY < TMXMapLayer.getHeight() 
     && spotX >= 0 && spotY >= 0) { 
     /* Set player velocity */ 
final Vector2 velocity = Vector2Pool.obtain(pValueX * 3, pValueY * 3); 
PlayerBody.setLinearVelocity(velocity); 
Vector2Pool.recycle(velocity); 
/* Math to determine the direction of movement */ 
double dirDouble = (Math.atan2(pValueX, pValueY) 
    /(Math.PI/2) + 2); 
float direction = (int) Math.round(dirDouble) 
    % DirectionState; 
     /* if movement is N,E,S or W, do animation */ 
    if (direction == 0) { // If Go North 
    player.animate(new long[] { 200, 200, 200 }, 0,2, true); // north 

    } else if (direction == 1) { // If Go West 
    player.animate(new long[] { 200, 200, 200 }, 9,11, true); // west 

    } else if (direction == 2) { // If Go South 
    player.animate(new long[] { 200, 200, 200 }, 6,8, true); // south 

    } else if (direction == 3) { // If Go East 
    player.animate(new long[] { 200, 200, 200 }, 3,5, true); // east 
    } 
     } 
    } 
+0

是什麼DirectionState變量? – user2080866 2013-02-20 15:53:11

回答

3

你使用此代碼

if (direction == 0) { // If Go North 
player.animate(new long[] { 200, 200, 200 }, 0,2, true); // north 

} else if (direction == 1) { // If Go West 
player.animate(new long[] { 200, 200, 200 }, 9,11, true); // west 

} else if (direction == 2) { // If Go South 
player.animate(new long[] { 200, 200, 200 }, 6,8, true); // south 

} else if (direction == 3) { // If Go East 
player.animate(new long[] { 200, 200, 200 }, 3,5, true); // east 
} 

你沒有如果方向重新覈實每更新動畫已經改變了,爲了糾正這個問題,你應該添加一個外部if來檢查方向是否已經改變。您的代碼應該像

if(direction != lastDirection){ 
    lastDirection = direction; 
    if (direction == 0) { // If Go North 
    player.animate(new long[] { 200, 200, 200 }, 0,2, true); // north 

    } else if (direction == 1) { // If Go West 
    player.animate(new long[] { 200, 200, 200 }, 9,11, true); // west 

    } else if (direction == 2) { // If Go South 
    player.animate(new long[] { 200, 200, 200 }, 6,8, true); // south 

    } else if (direction == 3) { // If Go East 
    player.animate(new long[] { 200, 200, 200 }, 3,5, true); // east 
    } 
} 

變量lastDirection應該是外部給你onControlChange範圍