简体   繁体   English

如何处理Java Applet中的多个按键?

[英]How do I handle multiple key presses in a Java Applet?

I am teaching myself, from online tutorials, how to write games in Java. 我正在通过在线教程自学如何用Java编写游戏。 I am using Java Applets to create a Pong game. 我正在使用Java Applets创建Pong游戏。 each paddle is controlled from different keys for 1v1 competition. 每个1v1比赛用不同的按键控制每个球拍。 this works fine if both users are hitting the keys at different times. 如果两个用户都在不同的时间点按了键,这将很好地工作。 but when one key is being held down and then another key is held down(ex: holding down on the arrow key, then user 2 holds the 'S' key), the second key overrides the first and the first paddle will stop moving. 但是当按住一个键然后按住另一个键(例如:按住箭头键,然后用户2按住“ S”键)时,第二个键将覆盖第一个键,并且第一个拨片将停止移动。 i'm guessing that i need to use threads but i don't know much about them and i am having trouble understanding how to use/implement them. 我猜想我需要使用线程,但是我对线程了解不多,而且在理解如何使用/实现线程时遇到了麻烦。 how would i go about handling the case when two (or more) keys are being held down? 当按住两个(或多个)键时,我该如何处理?

Bonus: like i said i don't know much about threads - i'm assuming i also need one for the ball/puck to be moving around while all else is going on. 奖金:就像我说的那样,我对螺纹的了解不多-我假设我还需要一个螺纹使球/冰球在其他所有情况下都可以运动。 is the right and if so how do i put a thread on something that takes no input? 是正确的,如果是的话,我该如何在不需要输入的东西上放置线程?

Thanks for you help, DJ 谢谢您的帮助,DJ

Usually instead of threads, games use something called the game loop (google it). 通常,游戏使用线程(而不是线程)代替Google的线程(使用Google循环)。

http://en.wikipedia.org/wiki/Game_programming http://en.wikipedia.org/wiki/Game_programming

The basic idea is 基本思想是

 Loop
    Get all inputs
    Get game clock
    Update state based on clock and inputs
    Update Display
    Limit frame rate if you need to

Anyway -- instead of getting keyboard events -- you check the keyboard's state at certain points. 无论如何-而不是获取键盘事件-您可以在某些点检查键盘的状态。 This code seems to do it for Java 此代码似乎适用于Java

http://www.gamedev.net/reference/articles/article2439.asp http://www.gamedev.net/reference/articles/article2439.asp

It uses events to set variables you look at in your loop. 它使用事件来设置您在循环中查看的变量。

What you usually do is to remember the state of every keypress. 通常,您要做的是记住每次按键的状态。

You keep an array of your actions(or an array of all the keys if you want too). 您可以保留一系列操作(如果需要,也可以保留所有键的数组)。 A keyDown event results in eg 一个keyDown事件导致例如

boolean actions[12];...
...

public boolean keyDown( Event e, int key ) {
 if (key == 'a') {
    actions[ACTION_LEFT] = true; 
  }
..
}

And you'll need to catch the keyup event and set the actions to false when the keys are released. 并且您将需要捕获keyup事件,并在释放键时将操作设置为false。

In the movement logic you can just check the states of the keypresses 在运动逻辑中,您可以仅检查按键的状态

if(actions[ACTION_LEFT] == true)
   moveLeft();
if(actions[ACTION_RIGTH] == true)
   moveRight();

Just in case anyone wanted to see how I ended up answering this question. 万一有人想看看我最终如何回答这个问题。 My keyDown() and keyUp() method are as follows: 我的keyDown()和keyUp()方法如下:

public boolean keyDown(Event e, int key)
{
        message = key + "pressed";

        //right paddle
        if(key == 1005)
        {
            rpaddle_up = true;
        }
        if(key == 1004)
        {
            rpaddle_down = true;
        }

        //left paddle
        if(key == 115)
        {
            lpaddle_up= true;
        }
        if(key == 119)
        {
            lpaddle_down=true;
        }

        //x key = exit
        if(key == 'x')
            System.exit(0);
        return true;        
    }

public boolean keyUp(Event e, int key)
    {
        //right paddle
        if(key == 1005)
        {
            rpaddle_up = false;
        }
        if(key == 1004)
        {
            rpaddle_down = false;
        }

        //left paddle
        if(key == 115)
        {
            lpaddle_up= false;
        }
        if(key == 119)
        {
            lpaddle_down=false;
        }
        return true;
    }

Hopefully if someone has the same issue this will help them. 希望有人遇到同样的问题会有所帮助。 And thanks everyone for your input and help. 并感谢大家的投入和帮助。

If I'm not mistaken, you have the keyUp() and keyDown() methods at your disposal with Applet. 如果我没记错的话,Applet可以使用keyUp()和keyDown()方法。 Have you tried setting a flag on keyDown, then unsetting it on keyUp? 您是否尝试过在keyDown上设置标志,然后在keyUp上取消设置标志? For example: 例如:

   public boolean keyDown( Event e, int key )
   {
      if (key == 'a') {
        player1.go("left")
      }
   }

   public boolean keyUp( Event e, int key )
   {
      if (key == 'a') {
        player1.stop("left")
      }
   }

Just an idea. 只是一个主意。 I'm sure there's a standard way to deal with this. 我敢肯定有一种标准的方法可以解决这个问题。

您可以使用Swing Timer定期触发keydown / keyup事件之间的移动事件。

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

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