【问题标题】:How to make key board commands in java如何在java中制作键盘命令
【发布时间】:2018-05-05 15:39:34
【问题描述】:

我刚开始和朋友一起制作 RPG。我们希望能够使用箭头键或 WASD 来移动角色。我不知道该怎么做,我一直在网上找了一会儿。我知道我必须使用 ActionListener,但除此之外,我不知道我在用它做什么。这是使用键盘输入的代码部分,但我不知道该怎么做

public class Character implements ActionListener
 {
  public static char KBoard(char w, char a, char s, char d)
   {
     /_\-|(This is what I need help with, reading in the keyboard input)|-/_\
     return kBoard;
    }
   public static int MoveX (int velocity)
    {
     velocity = 5

     Switch (kBoard)

     case 'w':
     characterPosX -= velocity;
     break;

     case 's':
     characterPosX += velocity;
     break;

     return characterPosY;
    }
   public static int MoveY (int velocity)
    {
     velocity = 5

     Switch (kBoard)

     case 'a':  
     characterPosY -= velocity;
     break;

     case 'd':    
     characterPosY += velocity;
     break;
     return characterPosY;
    }
   public void Character ()
    {
      (code to make the character appear)
    }
   }

【问题讨论】:

  • All-left 格式不好,请缩进代码 - 整理 switch 语句使其可行,返回什么以及 characterPosY 在哪里?
  • 所以,characterPosY 返回按下 'd' 或 'a' 按钮后角色的新 Y 位置,并将其返回给方法 Character,改变 Y位置。与 characterPosY 相同。我马上就会开始格式化。

标签: java input bluej direction


【解决方案1】:

您不需要动作监听器。 您需要使用扫描仪——不断读取输入的扫描仪。

public static char KBoard(char w, char a, char s, char d)
{
    Scanner scan = new Scanner(System.in);
    System.out.print("");
    char kBoard = scan.nextChar();
    return kBoard;
}

但是,嘿,现在如果有人需要知道,他们可以看这里。您还可以在此处添加一个过滤器,一个 if-else 语句,以确保传入的唯一字符是您想要的字符,而其他字符都不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多