有没有办法让 Space Invaders 键绑定,这样当我射击时它不会停止移动?

Is there a way to make Space Invaders keybindings so that when i shoot it doesn't stop the movement?

我有一个 Java Space Invaders 游戏使用按键绑定进行控制;如何防止射击停止运动?

最初我使用的是按键侦听器,但我在某处读到按键绑定可以解决这个问题(以及另一个我在初始移动时有滞后的问题)但它没有帮助。澄清一下,none 个字符是一个 JComponent,但键绑定在 JFrame window 上注册并且它们有效。

// 创建一个 window window = 新 Window();

    DataStorageObject data = new DataStorageObject(window);

    //Keybinding code

    Action leftAction = new AbstractAction() 
    {
    @Override
    public void actionPerformed(ActionEvent arg0) 
        {
    if(!data.paused)
            {
    if(data.usership.getXPos() != 0)
    data.usership.setXPos(data.usership.getXPos() - 15);
            }
        }
    };

    Action shootAction = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
    if(!data.paused) {
    if(data.cooldown == 0)
    {
    // fire a bullet
    PositionObject bullet = new PositionObject();
    bullet.setXPos(data.usership.getXPos() + data.window.usershipimage.getWidth() / 2);// TODO figure out the right number
    bullet.setYPos(data.usership.getYPos());// TODO figure out the right number
    data.userbullets.add(bullet);

    // start cooldown
    data.cooldown = 30; // TODO figure out the right number
    }
    }
    }};

    Action rightAction = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
    if(!data.paused) {
    if(!(data.usership.getXPos() >= data.window.INVADERSWIDTH - data.window.usershipimage.getWidth()))
    data.usership.setXPos(data.usership.getXPos() + 15);
    }
    }};

    Action pauseAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            data.paused = !data.paused;
        }};



    KeyStroke space = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0);
    KeyStroke left = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0);
    KeyStroke right = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0);
    KeyStroke p = KeyStroke.getKeyStroke(KeyEvent.VK_P,0);

    //random component solution 
    JLabel listener= new JLabel();
    JFrame frame = new JFrame();
    window.add2Container(listener);
    //figure out where put focus and what parameter for mapname
    window.requestFocus();

    int mapName = JComponent.WHEN_IN_FOCUSED_WINDOW;

    InputMap inputMap = listener.getInputMap(mapName);

    inputMap.put(space, "shoot");
    inputMap.put(left, "left");
    inputMap.put(right, "right");
    inputMap.put(p, "pause");

    ActionMap amap = listener.getActionMap();

    amap.put("shoot", shootAction);
    amap.put("left", leftAction);
    amap.put("right", rightAction);
    amap.put("pause", pauseAction);

我找到了解决这两个问题的有效方法;我没有直接从按键触发船舶移动和射击,而是创建了三个与控件相对应的布尔值,如果每个布尔值都为真,则将主体方法移动到游戏循环中以发生,并且在按下或释放布尔值时变为真或假。现在唯一的问题是船移动得非常快,所以我可能不得不放慢游戏循环或其他东西。