如何让多个Key Binding同时工作? [英] How can i make multiple Key Bindings work at the same time?

查看:126
本文介绍了如何让多个Key Binding同时工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设计一个有两个玩家的游戏。每个都有一个球,应该能够向右或向左移动球,第一个玩家带有'a''d'按钮,第二个玩家有右箭头按钮。然而,目前一名球员需要等待其他球员的动作完成才能移动他们自己的球。我该如何解决这个问题?以下是我的代码的相关部分:

I need to design a game with two players. Each has a ball and should be able to move the ball to right or left, the first player with 'a' 'd' buttons and the second player with right,left arrow buttons. However currently one player needs to wait for the other player's action to be completed in order to move their own ball. How can i resolve that problem? Here is the related parts of my code:

    public class AnimationWindow extends JPanel{

      public AnimationWindow()
        {

            super();
            ....
            ....
            cezmiMover();

        } 



public void cezmiMover(){

        this.getInputMap().put(KeyStroke.getKeyStroke('a'), "left1");
        this.getActionMap().put("left1", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {

                board.cezmi1.moveLeft();
            }
        });

        this.getInputMap().put(KeyStroke.getKeyStroke('d'), "right1");
        this.getActionMap().put("right1", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {

                board.cezmi1.moveRight();
            }
        });

        this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "left2");
        this.getActionMap().put("left2", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {

                board.cezmi2.moveLeft();
            }
        });

        this.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "right2");
        this.getActionMap().put("right2", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {

                board.cezmi2.moveRight();
            }
        }); 
    }
}


推荐答案

你需要使用一系列标志和某种更新循环来根据标志的状态更新游戏状态...

You need to use a series of flags and some kind of "update" loop to update the state of the game depending on the state of the flags...

例如,首先创建一系列标志...

For example, start by creating a series of flags...

private boolean p1Left, p1Right, p2Left, p2Right = false;

这些可以通过单个玩家对象轻松维护,但是你没有提供那么多代码...

These could just as easily be maintained by the individual player objects, but you've not provided that much code...

接下来,您需要监控按键和按键释放事件,并根据需要设置标志的状态...

Next, you need to monitor for key press and key release events and set the state of the flag as required...

this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "right1down");
this.getActionMap().put("right1down", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        p1Right = true;
    }
});

this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "right1up");
this.getActionMap().put("right1up", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        p1Right = false;
    }
});

然后你需要一些可以更新游戏状态的循环或计时器。就个人而言,我喜欢使用 javax.swing.Timer ,但这只是我。

Then you need some kind loop or timer that can update the state of the game. Personally, I like using javax.swing.Timer, but that's just me.

每次运行更新时循环,你需要检查每个标志的状态并相应地更新对象...

On each run of the update loop, you need to check the state of each flag and update the objects accordingly...

if (p1Right) {
    board.cezmi1.moveRight();
}

example

这篇关于如何让多个Key Binding同时工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆