2

我有一个问题,当我按下 Return 键时,在 INTRO 状态下(默认)它必须切换到 GAME 状态。确实如此,但当它出现时,它会在 INTRO 和 GAME State 之间闪烁。文本闪烁,底部的条闪烁。

我会尝试打印出当前状态,它似乎在这两种状态之间没有变化,但是当我运行应用程序时,我的 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);

}
4

1 回答 1

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
}
于 2012-12-07T20:49:05.257 回答