Search code examples
key-bindings

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


I have a Java Space Invaders game using keybindings for controls; How can I prevent shooting from stopping movement?

Originally i used keylisteners, but i read somewhere that keybinding would solve this issue (as well as another i have of a lag in initial movement) but it hasn't helped. To clarify, none of the characters is a JComponent, but the keybindings are registered on the JFrame window and they work.

// create a window window = new 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);

Solution

  • I found a working solution to both problems; instead of triggering ship movement and shooting directly from the keypress, i created three booleans corresponding to the controls,moved the action method bodied into the game loop to happen if each boolean is true, and on press or release the booleans become true or false. the only issue now is that the ship moves very fast, so i might have to slow down the game loop or something.