2012-12-07 2 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

물론, 오신 것을 환영합니다! –