0
我想寫一個動畫序列的邏輯,並且似乎無法獲得正確的東西。我想要發生的事情是:如果用戶點擊屏幕,該方法將採用touchEvent座標,然後更改精靈的運動變量,以便精靈傳播到用戶觸摸屏幕的位置。我有這樣的「啓動」事件設置。Android動畫問題
public void launch(float eventX, float eventY) {
//get the touch event coords and then move the banana to them.
fire = true;
//the x and y variables for the sprite
getX();
getY();
//the target x and y variables
targetX = eventX;
targetY = eventY;
//the total distance the two variable have to "travel"
distanceX = x - targetX;
distanceY = y - targetY;
//variables to update the movement
moveX = distanceX;
moveY = distanceY;
}
然後,我想我應該把運動參數的更新方法是這樣的:
public void update(long gameTime) {
if(gameTime > frameTicker + framePeriod) {
frameTicker = gameTime;
currentFrame++;
if(currentFrame >= frameNbr){
currentFrame = 0;
}
}
this.sourceRect.left = currentFrame * spriteWidth;
this.sourceRect.right = this.sourceRect.left + spriteWidth;
if(fire == true){
x = (int) moveX;
y = (int) moveY;
}
如果用戶點擊,因爲它是動畫顯示像它應該,但是隨後會立即轉到屏幕的左上角,或者我已經瞭解的是座標系統上的(0,0)。我無法弄清楚如何減慢它的速度,使它在合理的空間移動並走到它應該達到的位置。