2010-03-10 21 views
2

我正在學習Java,我知道關於新手程序員的一個大抱怨是我們製作了很長時間且涉及的方法,應該將其分解爲幾個。那麼這裏是我寫的一個例子。 :-D。在Java中重構此功能

public void buildBall(){ 

     /* sets the x and y value for the center of the canvas */ 
     double i = ((getWidth()/2)); 
     double j = ((getHeight()/2)); 

     /* randomizes the start speed of the ball */ 
     vy = 3.0; 
     vx = rgen.nextDouble(1.0, 3.0); 
     if (rgen.nextBoolean(.05)) vx = -vx;  

     /* creates the ball */ 
     GOval ball = new GOval(i,j,(2 *BALL_RADIUS),(2 * BALL_RADIUS));  
     ball.setFilled(true); 
     ball.setFillColor(Color.RED); 
     add(ball); 


     /* animates the ball */ 
     while(true){ 
      i = (i + (vx* 2)); 
      j = (j + (vy* 2)); 
      if (i > APPLICATION_WIDTH-(2 * BALL_RADIUS)){ 
       vx = -vx;  
      } 

      if (j > APPLICATION_HEIGHT-(2 * BALL_RADIUS)){ 
       vy = -vy; 
      } 

      if (i < 0){ 
       vx = -vx; 
      } 

      if (j < 0){ 
       vy = -vy; 
      } 

      ball.move(vx + vx, vy + vy); 
      pause(10); 

      /* checks the edges of the ball to see if it hits an object */ 
      colider = getElementAt(i, j); 

      if (colider == null){ 
       colider = getElementAt(i + (2*BALL_RADIUS), j); 
       } 
      if (colider == null){ 
       colider = getElementAt(i + (2*BALL_RADIUS), j + (2*BALL_RADIUS)); 
       } 
      if (colider == null){ 
       colider = getElementAt(i, j + (2*BALL_RADIUS)); 
       } 

      /* If the ball hits an object it reverses direction */ 
      if (colider != null){ 
       vy = -vy; 


      /* removes bricks when hit but not the paddle */ 
       if (j < (getHeight() -(PADDLE_Y_OFFSET + PADDLE_HEIGHT))){ 
       remove(colider); 
       } 

      } 

     } 

你可以從方法的標題看到,我開始時有着「造球」的好意。

有我撞到的幾個問題:

的問題是,然後我需要移動球,所以我創建了一個while循環。我沒有看到任何其他的方式去做,除了保持「真實」,所以這意味着我在這個循環下創建的任何其他代碼都不會發生。我沒有讓while循環變成一個不同的函數,因爲我使用了這些變量i and j

所以我沒有看到我可以重構這個循環之外。

所以我的主要問題是:

我將如何傳遞的i and j值,以一種新的方法:「animateBall」和我將如何使用新的方法 ball.move(vx + vx, vy + vy);如果球已經在buildBall方法被宣佈?

我理解這可能是更好的理解變量的作用域和參數傳遞一個簡單的事情,但我還沒有應用...

+0

嗯...這個詞是拼寫爲 「撞機」 而不是 「colider」。你的代碼有很多風格問題。 – 2010-03-10 07:01:48

+1

恩,謝謝你的幫助? – Joel 2010-03-10 07:07:46

+0

作爲一個你真正想要處理'球'對象的人,你能說得更具體嗎?比如說,你有一個球形物體,你覺得它有多棒,比如球形物體應該採用什麼方法?這樣提供解決方案會更容易。 – Zaki 2010-03-10 07:17:17

回答

3

這可以重構爲三種方法 a> build ball:創建球對象並設置初始位置:buildBall() b>使整個while循環動畫(除了對碰部分動畫)(球,vx,vy) c>獲取collider getCollider()

由於球是一個對象,並且您已經設置了i,j作爲它的字段,它們將被傳遞。 Java按值傳遞所有參數。對象生活在堆上;對象引用通過值作爲方法參數傳遞。

編輯,添加僞代碼

class Animator{ 
    void animateBall(){ 
    Ball ball = buildBall(); //Ball will have i,j,radius etc set by this method 
    int vx = randomNumber(); 
    int vy = randomNumber(); 
    moveIt(vx,vy, ball); 
    } 
    void moveIt(int vx, int vy, Ball ball){ 
     while(true){ 
      //move the ball, change i,j fields of ball 
      //check for collission etc 
      Collider collider = getCollider(ball); 
      //change direction based on collider etc. 
     } 
    } 
    Collider getCollider(Ball ball){ 
    //collision code here 
    } 
} 
+0

對 - 我明白了。我的問題是1)如何將構建球的參數傳遞給動畫球,以及2)如果前一個函數是一個永遠不會變爲false的while循環,它如何到達碰撞器? – Joel 2010-03-10 07:19:17

+0

@Joel,添加了一些僞代碼,希望這有助於 – saugata 2010-03-10 08:16:38

+0

這有助於很多。感謝您抽出寶貴的時間! – Joel 2010-03-10 08:20:50

1

這是少了幾分多餘

 colider = getElementAt(i, j); 

      /* If the ball hits an object it reverses direction */ 

     if (colider != null && j < (getHeight() -(PADDLE_Y_OFFSET + PADDLE_HEIGHT))) 
     { 
      vy = -vy; 

     /* removes bricks when hit but not the paddle */ 
      remove(colider); 
     } 


      else 
      { 
       colider = getElementAt(i + (2*BALL_RADIUS), j); 
       colider = getElementAt(i + (2*BALL_RADIUS), j + (2*BALL_RADIUS)); 
       colider = getElementAt(i, j + (2*BALL_RADIUS));  
      } 
+0

不錯,謝謝! – Joel 2010-03-10 07:09:41