2013-07-18 40 views
0

我正在製作一個應用程序,並且它有一定的工作能力,但幸好我知道我的問題是什麼。我有一個圍繞邊界移動的精靈,我想讓這個精靈在特定的地點射擊一個球。目前,我的應用程序在位置(0,0)處顯示球,然後在應有的位置將其射出。它通過在更新方法中將10添加到x來實現此目的,因此它的動畫可以通過改變10來加快它的速度。變量x和y的默認值爲0.我不是像上面指定的那樣更改它們。我希望他們保持0作爲默認值,但我不想讓球顯示,直到它移動。現在它在啓動時顯示球,然後移動。我希望它在顯示x和y值時顯示球。隱藏圖像,直到給出值

以下是當前代碼片段。

 // This moves the ball down the right side of screen 
     if (x > ov.getWidth() - width - xSpeed){ 
      xSpeed = 0; 
      ySpeed = 10; 
      direction = 1; 
      shootL(); // shoots the ball left towards the left side of screen 

     } 

       // moves ball up the screen on left side 
     if (x + xSpeed < 0){ 
      x = 0; 
      xSpeed = 0; 
      ySpeed = -10; 
      direction = 3; 
      shootR(); // shoots the ball towards right side of screen 
     } 


    private void shootR() { 
     // TODO Auto-generated method stub 
     // need to manipulate variable q to go across screen 
     q += 10; 
      // eventually manipulate r so that it shoots at the right spot on screen, not my major problem right now. 

    } 


    private void shootL() { 
     // TODO Auto-generated method stub 
     // need to manipulate variable q to go across screen 
     q += -10; 
      // eventually manipulate r so that it shoots at the right spot on screen, not my major problem right now. 
    } 


    public void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     update(); 
     int srcY = direction * height; 
     Rect src = new Rect (0, srcY, width, srcY + height); 
     Rect dst = new Rect (x, y, x+width, y+height); 
     canvas.drawBitmap(b, src, dst, null); // draws sprite around border 
     Paint p = new Paint(); 
     canvas.drawBitmap(fire, q, r, p); // draws ball that shoots across 


    } 

任何人有任何意見或建議嗎?它只是在錯誤的時間顯示球,我需要解決這個問題。必須是一個錯誤或什麼。

+0

假設球是佈局xml文件的一部分,除非您提供了x和y座標,否則您可以將該視圖保持爲INVISIBLE或GONE。這不是這種情況嗎? – Rajeev

+0

@Rajeev,我正在做一個表面視圖,而不是一個XML佈局。 – Patrick

+0

是的...我看到了你的答案。我在發佈答案之前發表了評論。你應該接受你的答案。 – Rajeev

回答

0

哇,我無能爲力。我想到了。我更改了onDraw方法中調用的更新方法中的座標。 onDraw方法遍歷每一行代碼,然後再次被調用一次更新。通過在onDraw中添加一個if語句並刪除更新中的q的變化並將其移至if語句中,直到if爲true時才繪製它。

在我改變之前,我正在繪畫。您需要更改變量,然後繪製!