我的更新方法爲持續更新。移動位圖不平滑,生澀
@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或更高的時候,這個動作就更大了。有沒有什麼方法可以使運動更流暢?
我認爲你工作太辛苦。 [Lerp](http://developer.android.com/reference/android/view/animation/LinearInterpolator.html)似乎完成了你想要完成的大部分工作。 – Thomas 2012-03-14 16:18:35