简体   繁体   English

LWJGL按住一键

[英]LWJGL holding a key pressed

So basicaly what im trying to do is using LWJGL now making my player move in the game. 所以我基本上想要做的就是使用LWJGL让我的玩家在游戏中移动。 The player is currently moving but he doesnt keep mooving while you are holding the button.. 玩家目前正在移动,但是当你按住按钮时他并没有继续移动..

public void update(){ while(Keyboard.next()){ public void update(){while(Keyboard.next()){

        if(Keyboard.getEventKey() == Keyboard.KEY_RIGHT) {
            if(Keyboard.getEventKeyState()){
                System.out.println("KEY DOWN!");
                player.playerMovingRight();
            }
            else{
                System.out.println("KEY RELEASED!");
            }
        }
    }

I tried using a while(Keyboard.getEventKeyState()) but it just made the game crash and it wouldnt recognise if i release the key. 我尝试使用一段时间(Keyboard.getEventKeyState()),但它只是让游戏崩溃,它不会识别我是否释放密钥。

So how can i make my player keep moving if you hold the button instead of having to rapidly press the key to move. 那么如果按住按钮而不必快速按下键移动,我怎样才能让我的玩家继续前进。 And how would i make it work if the player is holding 2 buttons at the same time? 如果玩家同时按住2个按钮,我将如何使其工作?

Updated code: 更新的代码:

public void update(){
    while(Keyboard.next()){

        //If key escape is down we shut the application down
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            System.exit(0);
        }

        //If key up was pressed move up
        else if(Keyboard.getEventKey() == Keyboard.KEY_UP) {
            if(Keyboard.getEventKeyState()){
                System.out.println("KEY DOWN!");
                moveUp = true;
            }
            else{
                System.out.println("KEY RELEASED!");
                moveUp = false;
            }
        }
        //If key down was pressed move down
        else if(Keyboard.getEventKey() == Keyboard.KEY_DOWN) {
            if(Keyboard.getEventKeyState()){
                System.out.println("KEY DOWN!");
                moveDown = true;
            }
            else{
                System.out.println("KEY RELEASED!");
                moveDown = false;
            }
        }

        if(moveUp == true){
            player.playerMovingUp();
        }
        if(moveDown == true){
            player.playerMovingDown();
        }
    }

Im still having the same problem with the code, Im starting to think its they Keyboard.next() that is preventing me from holding the button and the player still moves. 我仍然对代码有同样的问题,我开始认为它们是Keyboard.next()阻止我按住按钮而玩家仍在移动。

What you need is a boolean flag which triggers when the button is pressed down, and is untriggered when the button is released. 你需要的是一个布尔标志,当按下按钮时触发,当释放按钮时不触发。 The flag would then be added to an if statement before the movement code. 然后将该标志添加到移动代码之前的if语句中。 Like so: 像这样:

if(Keyboard.getEventKey() == Keyboard.KEY_RIGHT) {
        if(Keyboard.getEventKeyState()){
            System.out.println("KEY DOWN!");
            flag = true;       
        }
        else{
            System.out.println("KEY RELEASED!");
            flag = false;
        }
    }
}

//player movement code

if(flag) {
    player.playerMovingRight();
}

The way to do it with multiple keys is to use an array of boolean triggers and repeating the checks for each key. 使用多个键执行此操作的方法是使用布尔触发器数组并重复检查每个键。

While the above answer will work, LWJGL has the capability to detect if a key is being held down. 虽然上述答案可行,但LWJGL能够检测是否按下了按键。 It is not enabled by default though, so you need to enable it. 但是默认情况下不启用它,因此您需要启用它。 To do this just set this flag to true somewhere in your input initialization. 要做到这一点,只需在输入初始化的某处将此标志设置为true。

Keyboard.enableRepeatEvents(true);

Now in your event loop, just do this check on your keys to see if it is a repeat (held down key). 现在在您的事件循环中,只需检查您的密钥以查看它是否是重复(按住键)。

if(Keyboard.isRepeatEvent())

Here is a small sample to see how to check a key for different key states. 这是一个小样本,了解如何检查不同关键状态的密钥。

while(Keyboard.next())
    {
        if(Keyboard.getEventKey() == Keyboard.KEY_A)
            if(Keyboard.getEventKeyState())
            {
                if(Keyboard.isRepeatEvent())
                    // Key held down
                else
                    // Key pressed
            }
            else
                // Key released
    }

while(Keyboard.next()) is supposed to be used with events. while(Keyboard.next())应该与事件一起使用。

In your case, you don't want to only do something, when an event gets triggered. 在您的情况下,您不希望只在事件被触发时执行某些操作。 But you want to determine many times per second, whether a key is pressed or not. 但是你想要确定每秒多次,无论是否按下了一个键。 If it is, you want to do something (eg move). 如果是,你想做某事(例如搬家)。

So it's the best solution to use the Keyboard.isKeyDown() function. 所以这是使用Keyboard.isKeyDown()函数的最佳解决方案。 A little example: 一个小例子:

if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
    System.out.println("pressing KEY_UP");
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
    System.out.println("pressing KEY_DOWN");
}
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
    System.out.println("pressing KEY_LEFT");
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
    System.out.println("pressing KEY_RIGHT");
}

Also, a good tutorial can be found here . 此外, 这里可以找到一个很好的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM