2012-03-14 108 views
1

我的更新方法爲持續更新。移動位圖不平滑,生澀

@Override 
public void update(float deltaTime) { 
    Graphics g = game.getGraphics(); 
    List<TouchEvent> touchEvents = game.getInput().getTouchEvents(); 
    game.getInput().getKeyEvents(); 

    redPin.moveX(deltaTime); 

    int len = touchEvents.size(); 
    for (int i = 0; i < len; i++) { 
     TouchEvent event = touchEvents.get(i); 
     if (event.type == TouchEvent.TOUCH_UP) { 
      if (inBounds(event, 0, 360, 800, 120)) { 
       if (gameOver) { 
        // TODO game over methods 
       } else { 
        // TODO game is not over methods 
        level++; 
        blackBacking.randomizeX(); 
        blackBacking.setWidth(level + 1); 
       } 
       return; 
      } 
     } 
    } 
} 

我的位圖類

public class RedPin { 
    private int xTracker = 400; 
    private int x = 400; 
    private int y = 85; 
    private int height = 111; 
    private long speed = 1000; 
    private float deltaTime; 
    private int direction = 1; 

    protected int getX() { 
     if (xTracker <= 86) { 
      x = -10; 
     } else if (xTracker >= 714) { 
      x = -10; 
     } else if (xTracker <= 713 && xTracker >= 87) { 
      x = xTracker; 
     } 
     return this.x; 
    } 

    protected int getY() { 
     if (this.xTracker == 86 || this.xTracker == 714) { 
      this.y = 88; 
     } else if (this.xTracker == 87 || this.xTracker == 713) { 
      this.y = 87; 
     } else if (this.xTracker == 88 || this.xTracker == 89 || this.xTracker == 712 || this.xTracker == 711) { 
      this.y = 86; 
     } else { 
      this.y = 85; 
     } 
     return this.y; 
    } 

    protected int getHeight() { 
     if (this.xTracker == 86 || this.xTracker == 714) { 
      this.height = 104; 
     } else if (this.xTracker == 87 || this.xTracker == 713) { 
      this.height = 105; 
     } else if (this.xTracker == 88 || this.xTracker == 712) { 
      this.height = 106; 
     } else if (this.xTracker == 89 || this.xTracker == 711) { 
      this.height = 107; 
     } else if (this.xTracker == 90 || this.xTracker == 710) { 
      this.height = 109; 
     } else if (this.xTracker == 91 || this.xTracker == 709) { 
      this.height = 110; 
     } else { 
      this.height = 111; 
     } 
     return this.height; 
    } 

    protected void moveX() { 
     if (this.xTracker >= 734) { 
      direction = 1; 
     } else if (this.xTracker <= 66) { 
      direction = -1; 
     } 
     xTracker = (int) direction * ((xTracker + 1)*deltaTime); 
     x = (int) direction * ((x + 1)*deltaTime); 
    } 

} 

g.drawPixmap(Assets.gameRedPin,redPin.getX(),redPin.getY(),0,0,2,redPin.getHeight());

當我用x + 1 * deltaTime運行代碼時,會出現一些輕微的動作。 當我把它設置爲x + 2或更高的時候,這個動作就更大了。有沒有什麼方法可以使運動更流暢?

+0

我認爲你工作太辛苦。 [Lerp](http://developer.android.com/reference/android/view/animation/LinearInterpolator.html)似乎完成了你想要完成的大部分工作。 – Thomas 2012-03-14 16:18:35

回答

0

你能記錄deltaTime的值嗎?這可能是因爲刷新速率太低(即20 FPS),或者deltaTime值有很大的變化,這兩者都會導致抖動。另外,如果你在模擬器上測試你的代碼,它肯定會出現問題。

+0

即使在物理設備上運行,但連接了調試器,可能會導致性能問題 – 2012-03-14 16:17:23

+0

我在Nexus S上運行。我讀了某處需要包含插值的地方? – 2012-03-14 16:18:21

+0

插值可能不會讓你的動畫更流暢,如果你可以發佈什麼樣的deltaTime被傳遞到這裏併發布在這裏,它可能會理解問題的根源。 – Kai 2012-03-14 16:25:25