2012-12-07 122 views
2

我有一個問題,當我按下Return鍵時,在INTRO狀態(默認情況下)它必須切換到GAME狀態。它確實如此,但是當它出現時,它會在INTRO和GAME State之間閃爍。文本閃爍,底部的欄閃爍。Java LWJGL閃爍狀態

我試圖打印出當前狀態,看起來它在這兩個狀態之間沒有變化,但是當我運行應用程序,我的INTRO狀態和GAME狀態時,它們都是閃爍的。

public Renderer() { 
    try { 
     Display.setDisplayMode(new DisplayMode(screenWidth, screenHeight)); 
     Display.setTitle(gameTitle); 
     Display.create(); 
    } catch (LWJGLException e) { 
     e.printStackTrace(); 
     System.exit(0); 
    } 

    GL11.glMatrixMode(GL11.GL_PROJECTION); 
    GL11.glLoadIdentity(); 
    GL11.glOrtho(0, 640, 0, 480, 1, -1); 
    GL11.glMatrixMode(GL11.GL_MODELVIEW); 

    lastFrame = getTime(); 


    while (!Display.isCloseRequested()) { 
     //long delta = (long) getDelta(); 

     System.out.println(state); 

     while (Keyboard.next()) { 
      if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)) { 
       if(state == State.INTRO) { 
        clearCanvas(); 
        state = State.GAME; 
       } 
      } 
      if(Keyboard.isKeyDown(Keyboard.KEY_BACK)) { 
       if(state == State.GAME) { 
        clearCanvas(); 
        state = State.INTRO; 
       } 
      } 
     } 

     switch (state) { 
     case INTRO: 
      GL11.glColor3f(1.0f, 1.0f, 1.0f); 
      TextDraw.drawString("PRESS 'RETURN' TO START GAME, and then hit CTRL z", Display.getWidth()/2 - 160, Display.getHeight()/2); 
      TextDraw.drawString("CONTROLS:", Display.getWidth()/2 - 30, Display.getHeight()/2 - 50); 
      TextDraw.drawString("1 key is Paint Red", Display.getWidth()/2 - 70, Display.getHeight()/2 - 70); 
      TextDraw.drawString("2 key is Paint Green", Display.getWidth()/2 - 70, Display.getHeight()/2 - 85); 
      TextDraw.drawString("3 key is Paint Blue", Display.getWidth()/2 - 70, Display.getHeight()/2 - 100); 
      TextDraw.drawString("CTRL Z is Clear canvas", Display.getWidth()/2 - 70, Display.getHeight()/2 - 115); 
      TextDraw.drawString("Escape to Close Program", Display.getWidth()/2 - 70, Display.getHeight()/2 - 130); 
      break; 
     case GAME: 
      keyBindings(); 
      int MouseX = Mouse.getX(); 
      int MouseY = Mouse.getY(); 
      paint(MouseX, MouseY); 
      break; 
     } 

     Display.update(); 
     Display.sync(250); 
    } 

    //Closed 
    System.out.println("Program Closed"); 
    Display.destroy(); 
    System.exit(0); 

} 

回答

1

嘗試更換這些部件:

while (Keyboard.next()) { 
     if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)) { 
      if(state == State.INTRO) { 
       clearCanvas(); 
       state = State.GAME; 
      } 
     } 
     if(Keyboard.isKeyDown(Keyboard.KEY_BACK)) { 
      if(state == State.GAME) { 
       clearCanvas(); 
       state = State.INTRO; 
      } 
     } 
    } 

與此:

if (Keyboard.isKeyDown(Keyboard.KEY_RETURN)){ 
    clearCanvas(); //Don't check for the state which is going to be true most likely, just adds confusion to coding 
    state = State.GAME; 
} 

if (Keyboard.isKeyDown(Keyboard.KEY_BACK)){ 
    clearCanvas(); 
    state = State.INTRO; 
} 

如果您擔心速度,如果不碰到按鍵,請不要擔心。差別非常小,並且很可能不明顯。

此外,你沒有清除屏幕!沒有這些,各種各樣的殘餘物可以留在那裏!使用此方法在主循環中清除:

while (!Display.isCloseRequested()){ 
    GL11.glClear(GL_COLOR_BUFFER_BIT); 
    //Rest of your code here 
} 
+0

哇,那實際上是做了這項工作! 關於while循環開始處的清除。我不能那樣做,因爲每次運行這個循環都會清除屏幕,不是嗎? 我使用clearCanvas();作爲清除畫布的方法。 謝謝! –

+0

當然,不客氣! –