2010-10-17 63 views
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)。我無法弄清楚如何減慢它的速度,使它在合理的空間移動並走到它應該達到的位置。

回答

1

如果需要,可以將整個動畫放入launch()函數中。

例如,在功能類似的端:

float incrementX = distanceX/100; 
float incrementY = distanceY/100; 
float spriteX = getX(); 
float spriteY = getY(); 
bool xDone = false; 
bool yDone = false; 

while(!(xDone && yDone)) { 
    if (distanceX <= spriteX) { 
    spriteX += incrementX; // update the sprite's x coordinate as well 
    } 
    if (distanceY <= spriteY) { 
    spriteY += incrementY; // update the sprite's y coordinate as well 
    } 
    try{ Thread.sleep(10) } catch(Exception e) {} 
} 

即代碼依賴於子畫面開始在較低的x和y大於事件;如果情況並非如此,則需要修改。