Java游戏步行+跳跃停止按键动作?

时间:2014-02-15 20:19:59

标签: java 2d

当我走路和跳跃时,在我的游戏中你好,它会在跳跃结束后停止一切:

not hotlinked image

你能看到跳跃之后,继续走一会儿吗?然后停下来? 它应该立即停止,但我的行走队列继续执行,因为我没有重置它,它应该只在按键或释放时重置。但是在我发布跳转键后,按键操作将停止工作。我想如果我一次使用2个按键并释放,操作系统将不记得我仍然按下的旧按键。即使在您的浏览器URL输入中,按住任意键并按住,然后按住另一个键以进行位和输入。释放它,它将不再写旧键。

有解决方法吗?我正在使用keylistener,因为我无法弄清楚如何找出KeyBinds中的密钥何时发布。

我就是这样做的:

public class Keyboard implements KeyListener {

    private Player player;

    public Keyboard(Player p) {
        this.player = p;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("yes");
        switch (e.getKeyCode()) {

            case KeyEvent.VK_LEFT:
                this.player.getMovement().moveLeft();
                break;
            case KeyEvent.VK_RIGHT:
                this.player.getMovement().moveRight();
                break;
            case KeyEvent.VK_SPACE:
                this.player.getMovement().jump();
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int left = KeyEvent.VK_LEFT;
        int right = KeyEvent.VK_RIGHT;
        if (e.getKeyCode() == left || e.getKeyCode() == right) {
            this.player.getMovement().stopMovement();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

}

如何解决此问题?如果你需要更多的代码部分,比如跳​​跃或行走队列,请告诉我。

键盘侧录:

Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Right clicked!
Space is a nice button

1 个答案:

答案 0 :(得分:2)

请记住在单独的线程中处理所有按键。

http://wiki.processing.org/w/Multiple_key_presses

方法#1

/**
multiplekeys taken from http://wiki.processing.org/index.php?title=Keep_track_of_multiple_key_presses
@author Yonas Sandbæk http://seltar.wliia.org
*/

// usage: 
// if(checkKey("ctrl") && checkKey("s")) println("CTRL+S");  

boolean[] keys = new boolean[526];
void draw(){}
boolean checkKey(String k)
{
  for(int i = 0; i < keys.length; i++)
    if(KeyEvent.getKeyText(i).toLowerCase().equals(k.toLowerCase())) return keys[i];  
  return false;
}

void keyPressed()
{ 
  keys[keyCode] = true;
  println(KeyEvent.getKeyText(keyCode));
}

void keyReleased()
{ 
  keys[keyCode] = false; 
}

Methord#2(这是一种更简单的方法,只需检查几个键(没有数组))

boolean keyup = false;
boolean keyright = false;
boolean keyleft = false;
boolean keydown = false;

float x,y;

void setup() {
  size(640,360);  
  x = width/2;
  y = height/2;
}

void draw() {
  background(51);
  fill(255);
  ellipse(x,y,16,16);

  if (keyup) y--;
  if (keydown) y++;
  if (keyleft) x--;
  if (keyright) x++;


}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) keyup = true; 
    if (keyCode == DOWN) keydown = true; 
    if (keyCode == LEFT) keyleft = true; 
    if (keyCode == RIGHT) keyright = true; 
  }
}

void keyReleased() {
  if (key == CODED) {
    if (keyCode == UP) keyup = false; 
    if (keyCode == DOWN) keydown = false; 
    if (keyCode == LEFT) keyleft = false; 
    if (keyCode == RIGHT) keyright = false; 
  }
}

方法#3

/**
Modified version of Option 1 multiplekeys (should provide improved performance and accuracy)
@author Yonas Sandbæk http://seltar.wliia.org (modified by jeffg)
*/

// usage: 
// if(checkKey(KeyEvent.VK_CONTROL) && checkKey(KeyEvent.VK_S)) println("CTRL+S");  

boolean[] keys = new boolean[526];

void draw(){}

boolean checkKey(int k)
{
  if (keys.length >= k) {
    return keys[k];  
  }
  return false;
}

void keyPressed()
{ 
  keys[keyCode] = true;
  println(KeyEvent.getKeyText(keyCode));
  if(checkKey(CONTROL) && checkKey(KeyEvent.VK_S)) println("CTRL+S");
}

void keyReleased()
{ 
  keys[keyCode] = false; 
}

方法#5

  /**
   * Snappier multiple key detection using Primitive Collections Classes for Java http://pcj.sourceforge.net/
   * (standard Java Collection Objects can be used instead)
   * @author analogAI http://recursivepath.com/analogAI/
   */

   // usage:
   // if(this.checkKeysHeld(KeyEvent.VK_CONTROL) && this.checkKeysHeld(KeyEvent.VK_S)) println("CTRL+S");  

import bak.pcj.set.IntSet;
import bak.pcj.set.IntOpenHashSet;

public class HelloWorld extends PApplet {

  public IntSet keysheld = new IntOpenHashSet();
  /**
   * @param keycode key integer code, the value are constants defined in KeyEvent Class
   *                http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyEvent.html
   *                e.g. KeyEvent.VK_A for letter A
   *                     KeyEvent.VK_0 for number 0
   *                     KeyEvent.VK_SHIFT for shift button
   * @return true if the key is currently held down, false otherwise
   */

  public boolean checkKeysHeld(int keycode){
    return this.keysheld.contains(keycode);
  }

  public void keyPressed(){
    // add key to the list of keys held down
    // with processing, the KeyEvent object is always available as "keyEvent",
    // the getKeyChar() is already in the variable 'key', and getKeyCode() is in the variable 'keyCode'.
    this.keysheld.add(this.keyEvent.getKeyCode());
    println("key pressed: "+KeyEvent.getKeyText(this.keyEvent.getKeyCode()));
    println("keys in current held list: "+this.keysheld.toString());
  }

  public void keyReleased(){
    // remove key from the list of keys held down
    this.keysheld.remove(this.keyEvent.getKeyCode());
  }
}