如何解决Java一次监听多个按键的问题?

时间:2019-06-12 02:29:08

标签: java jframe keylistener pong drjava

我和我的搭档正在尝试为我们的计算机科学最终项目创建游戏Pong。我们创建了一个参考代码,其中可以向上和向下控制2个多维数据集,并且工作正常。尝试同时控制两个多维数据集时,会出现此问题(一次只能移动1个多维数据集)。我们要使两个多维数据集同时移动。

我们想这样说:

yPos - is the y position of the black cube 
xPos - is the x position of the black cube 
xPos2 - is the x position of the blue cube
YPos2 - is the y position of the blue cube

键:

A - Go up for black cube
Z - Go down for black cube
K - Go up for blue cube
M - go down for blue cube

我们尝试使用更复杂的版本,该版本使用了j标签动画。我们如何通过图形功能制作乒乓球游戏。但是我们不明白:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class PongHelpNeed extends JFrame implements KeyListener
{
  // booleans to tell which key is pressed
  boolean upKey;
  boolean downKey;
  boolean upKey2;
  boolean downKey2;

  // the position variables
  int yPos;           
  int xPos; 
  int xPos2;
  int yPos2;

  public PongHelpNeed ()
  {
    //create window
    super ("Controller");
    setSize (660, 700);

    // set keys to false and original positions
    upKey = false;
    downKey = false;

    upKey2 = false;
    downKey2 = false;

    xPos = 100;
    yPos = 350;

    xPos2 = 500;
    yPos2 = 350;

    // add the frame as a listener to your keys
    addKeyListener (this);  
    // Show the frame
    setVisible(true);       
  }

  //needs to be here because the class implements KeyListener
  public void keyTyped (KeyEvent e)       
  {
    System.out.println (e.getKeyCode () + " Typed");
  }

  //needs to be here because the class implements KeyListener
  public void keyPressed (KeyEvent e) {
    //check if keys a,z or k,m are pressed

    if  (e.getKeyCode () == KeyEvent.VK_A) 
    {
      upKey = true;
    }
    else if (e.getKeyCode () == KeyEvent.VK_Z) 
    {
      downKey = true;
    }
    else if (e.getKeyCode () == KeyEvent.VK_K) 
    {
      upKey2 = true;
    }
    else if (e.getKeyCode () == KeyEvent.VK_M) 
    {
      downKey2 = true;
    }
    //repaint the window everytime you press a key
    repaint ();   
  } 

  //needs to be here because the class implements KeyListener
  public void keyReleased (KeyEvent e) 
  {
    System.out.println (e.getKeyCode () + " Released");
  }

  //paints the pictures 
  public void paint (Graphics g)   
  {
    //set background
    g.setColor(Color.WHITE);  
    g.fillRect(0, 0, 660, 700);

    //cube 1
    g.setColor(Color.BLACK);      
    g.fillRect(xPos,yPos,50, 50);

    //draw cube 2
    g.setColor(Color.BLUE);    
    g.fillRect(xPos2,yPos2, 50, 50);

    //if keys are pressed move the cubes accordingly up or down
    if (upKey == true)
    {
      yPos = yPos - 15;
      upKey = false;
    }

    else if (downKey == true)
    {
      yPos = yPos + 15;
      downKey = false;
    }
    else if (downKey2 == true){
      yPos2 = yPos2 + 15;
      downKey2 = false;
    }
    else if (upKey2 == true)  {
      yPos2 = yPos2 - 15;
      upKey2 = false;
    }   
  }

  public static void main (String[] args) 
  {
    new PongHelpNeed (); 
  }
} 

我们的预期结果是我们试图同时移动两个立方体。因此,当我们按下A键和K键时,黑色方块应该移动,蓝色立方体应该移动。

1 个答案:

答案 0 :(得分:0)

呼叫repaint()不会立即触发对paint的呼叫,因此keyPressed可能在paint之前被触发两次(或更多次)。

在您的paint方法中,您正在检查if-else中的键,这意味着如果其中一个标志为true,则不检查其余标志。您还会遇到keyPressed在标志上与paint交战的竞赛情况。另外,如果您快速多次按键,您将失去在第一个处理的事件和下一次重绘之间的所有多余按键。

与其在paint中进行移动,不如在keyPressed处理程序中进行移动。不要将标志设置为upKey = true;,但直接执行操作:yPos = yPos - 15;。然后paint方法将刷新视图以反映当前(更新)状态。