【问题标题】:Stop pause on key hold with KeyBindings Java使用 KeyBindings Java 停止暂停键保持
【发布时间】:2013-08-05 14:12:52
【问题描述】:

我正在尝试用 Java 编写 Pong 小程序。当用户向上或向下按住时,他们的桨应该平稳移动,但事实并非如此。它移动,然后暂停,然后再次开始移动。有没有办法阻止这种短暂的停顿发生?

主类:

public class Main extends JApplet {

    public DrawPanel dp = new DrawPanel(400, 400);

    public void init(){
        add(dp);
        setSize(400, 400);
        requestFocusInWindow();

        Action moveDown = new AbstractAction(){
            public void actionPerformed(ActionEvent e){
                dp.player.y += 10;
                dp.repaint();
            }
        };

        dp.getInputMap().put(KeyStroke.getKeyStroke("pressed DOWN"), "move down");
        dp.getActionMap().put("move down", moveDown);       
    }
}

DrawPanel 类:

public class DrawPanel extends JPanel {

    public Paddle player;
    public Paddle opponent;

    public DrawPanel(int height, int width){
        int y = (height / 2) - (height / 10);
        int w = 15;
        int h = height / 5;

        player = new Paddle(2, y, 15, h, Color.white);
        opponent = new Paddle(width - (w+2), y, 15, h, Color.white);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.BLACK);

        g.setColor(player.color);
        g.fillRect(player.x, player.y, player.width, player.height);

        g.setColor(opponent.color);
        g.fillRect(opponent.x, opponent.y, opponent.width, opponent.height);
    }   
}

桨类:

public class Paddle {

    public int x, y, width, height;
    public Color color;

    public Paddle(int _x_, int _y_, int w, int h, Color c){
        x = _x_;
        y = _y_;
        width = w;
        height = h;
        color = c;
    }
}

【问题讨论】:

  • 试试看this example。基本上,这样做是在鼠标按下/释放时引发一个标志,并允许更新引擎根据需要继续响应该标志
  • 我正在研究计时器解决方案,我想这就是我将要使用的解决方案
  • 不相关:绘画时不要改变组件的状态(在你的drawPanel中,就是setBackground)
  • 另外,考虑键绑定,如图here

标签: java swing applet key-bindings


【解决方案1】:

根本问题是 keyPressed 的“实例”概念与运动的“持续时间”概念之间的阻抗不匹配。

与其试图在视图中平滑它(这与游戏逻辑的细节无关),不如使用更适合的 api 来增强游戏模型。菲添加启动/停止逻辑并将这些方法绑定到 keyPressed/keyReleased:

public class Paddle {

     public void startMoveDown() {
         // here goes the logic, f.i. starting a timer
         // in the actionPerformed of that timer:
         ...  moveUnitDown(); 
     }
     public void stopMoveDown() {
        //... 
     }

     protected void moveUnitDown() {
         y+=unit;
         // ideally, have listeners and notify them the change of y
     } 
}

// in the view:
Action startMoveDown = new AbstractAction(){
    public void actionPerformed(ActionEvent e){
        player.startMoveDown();
   }
};

dp.getInputMap().put(KeyStroke.getKeyStroke("pressed DOWN"), "start move down");
dp.getActionMap().put("start move down", startMoveDown); 

// in the view:
Action stopMoveDown = new AbstractAction(){
    public void actionPerformed(ActionEvent e){
        player.stopMoveDown();
   }
};

dp.getInputMap().put(KeyStroke.getKeyStroke("released DOWN"), "stop move down");
dp.getActionMap().put("stop move down", stopMoveDown); 

【讨论】:

  • 我已经开始在按键时启动计时器并在按键释放时停止它,谢谢
猜你喜欢
  • 1970-01-01
  • 2015-06-25
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
相关资源
最近更新 更多