2015-01-07 49 views
0

當使用Eclipse IDE進行Java編程時,我有時會使用調試功能。很多時候,我喜歡在程序運行的同時實時更改變量。但是,我經常發現更改某些變量實際上不會影響當前正在運行的程序。Eclipse調試模式更改變量

我的問題是:是否有某些調試規則?哪些變量在調試器的範圍之內?

對不起,如果這是一個愚蠢的問題。我在Eclipse中調試相當新,而且一般編程。

下面的代碼是一個示例。我很抱歉,如果它很難閱讀或者其他問題,但這裏是問題:在Ball課程中,每當我修改最終變量如PARTICLES_PER_CLICKSPEED時,它們都會實時更新,我可以在程序窗口中看到不同之處,然而當我改變RADIUS變量時,它什麼都不做,即使它與其他兩個變量在同一個類中。

public class Main { 

    public static final int WIDTH = 1280; 
    public static final int HEIGHT = 720; 
    public static Ball[] ball = new Ball[100000]; 
    public Main() { 
     try { 
      Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); 
      Display.create(); 
      Display.setTitle("Game Engine"); 

     } catch (LWJGLException e) { 

      e.printStackTrace(); 
     } 

     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); 
     glMatrixMode(GL_MODELVIEW); 


     boolean[] doOnce = new boolean[10]; 
     boolean gravity = false; 
     while (!Display.isCloseRequested()) { 

      if (Mouse.isButtonDown(1) || Mouse.isButtonDown(1)) { 
       if (!doOnce[0]) { 
        Ball.createParticles(); 
        doOnce[0]=true; 
       } 

      } else { 
       doOnce[0] = false; 
      } 

      glClear(GL_COLOR_BUFFER_BIT); 

      for (int i = 1; i <= Ball.ballAmount; i++) { 
       ball[i].updatePosition(gravity); 
       if (Mouse.isButtonDown(0)) { 
        gravity = true; 
       } else { 
        gravity = false; 
       } 

       if (ball[i].position.x > 0 && ball[i].position.y > 0 && ball[i].position.x < WIDTH && ball[i].position.y < HEIGHT) { 
        glBegin(GL_TRIANGLE_FAN); 
        glVertex2d(ball[i].position.x, ball[i].position.y); 
        for(int u=0;u<=360;u+=5){ 
         glVertex2d(ball[i].position.x+Math.cos(u)*Ball.RADIUS, ball[i].position.y+Math.sin(u)*Ball.RADIUS); 
        } 
        glEnd(); 
       } 

      } 
      System.out.println("Particle Amount: " + Ball.ballAmount); 

      Display.update(); 
      Display.sync(60); 
     } 
     Display.destroy(); 
     System.exit(0); 
    } 

    public static void main(String[] args) { 
     new Main(); 
    } 
} 

class Ball { 

    public Vector2f position, velocity; 
    public static final int RADIUS = 10; 
    public static final int INITIAL_SPEED = 5; 
    public static final int SPEED = 2; 
    public static final int PARTICLES_PER_CLICK = 50; 

    public static int ballAmount = 0; 

    public double r, g, b; 

    public static Random rnd = new Random(); 

    public Ball(double x, double y) { 
     int angle = rnd.nextInt(360); 
     position = new Vector2f((float) x, (float) y); 
     velocity = new Vector2f((float) Math.cos(angle) * rnd.nextFloat() * INITIAL_SPEED, (float) Math.sin(angle) * rnd.nextFloat() * INITIAL_SPEED); 

     this.r = rnd.nextDouble(); 
     this.g = rnd.nextDouble(); 
     this.b = rnd.nextDouble(); 
    } 

    public void updatePosition(boolean gravity) { 
     this.position.x += this.velocity.x * SPEED; 
     this.position.y += this.velocity.y * SPEED; 
     if (gravity) { 
      double dx = this.position.x - Mouse.getX(); 
      double dy = this.position.y - (Main.HEIGHT - Mouse.getY()); 
      double distance = Math.sqrt(dx * dx + dy * dy); 

      this.velocity.x -= (this.position.x - Mouse.getX())/distance; 
      this.velocity.y -= (this.position.y - (Main.HEIGHT - Mouse.getY()))/distance; 
     } else { 
      this.velocity.x *= 0.99; 
      this.velocity.y *= 0.99; 
     } 

    } 

    public static void createParticles() { 
     for (int i = 1; i <= PARTICLES_PER_CLICK; i++) { 

      ballAmount += 1; 
      Main.ball[ballAmount] = new Ball(Mouse.getX(),Main.HEIGHT- Mouse.getY()); 
     } 
    } 

} 
+1

我想更詳細地解決這個問題,當我沒有按時間時,但通常情況下,您希望避免在調試過程中修改變量,並使值有機地出現。只有在**特殊**情況下,通過調試器注入值纔是一個好主意。 – Makoto

回答

1

如果優化器看到最終變量,它知道它的值不會改變。它對這些知識的作用取決於它。它可能什麼也不做(就像PARTICLES_PER_CLICK或SPEED似乎發生在你的情況中),或者它可能簡單地將該變量的所有事件都替換爲實際值。沒有特別的規定,超出不要更改最終變量的值