How to detect a button hold in a Java game?

Kenny :

I'm trying to make my first little game in Java. I want to make a ball, that moves down, if you dont press anything. But if you press the spacebar, it moves up with the same velocity, that it moves down. So just going up and down with one button.

The problem is, the ball doesn't go up smoothly. How can I fix this?

package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class Gameplay extends JPanel implements KeyListener, ActionListener{

    private boolean play = false;
    private int delay = 5;
    private Timer timer;

    private int playerX = 250;
    private int playerY = 325;
    private int playerYdir = 2;
    private boolean up = false;

    public Gameplay() { 
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        timer = new Timer(delay, this);
        timer.start();
    }

    public void paint(Graphics g) {
        //BG
        g.setColor(Color.black);
        g.fillRect(0, 0 , 1000, 700);
        //Player
        g.setColor(Color.yellow);
        g.fillOval(playerX, playerY, 30, 30);

        g.dispose();
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        timer.start();

        if(play) {
            if(playerY >= 640) playerY = 640;
            if(playerY <= 0) playerY = 0;
            if(!up) move(-playerYdir, false);
        }
        repaint();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            move(8, true);
            play = true;

        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            move(8, false);

        }
    }

    public void move(int moveUp, boolean dir) {
        //dir: true = down, false = up
        up = dir;
        playerY -= moveUp;
    }

    @Override
    public void keyTyped(KeyEvent e) {}
}

Michael :

You already have everything to detect that the button is held, but your approach to applying the movement is wrong. You detect when the key is pressed, and you detect when the key is released. All the time in-between those 2 events represents the key being held.

You should not apply the movement when the key is pressed, since that will only happen once. You should simply set a flag.

@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_SPACE) {
        isSpaceButtonDown = true;
    }
}

@Override
public void keyReleased(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_SPACE) {
        sSpaceButtonDown = false;
    }
}

Elsewhere in your game's main loop (the one that's called every frame), you should check the flag and act accordingly.

void myMainGameLoop() {
    if (isSpaceButtonDown) {
        moveUp();
    }
    else {
        moveDown()
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=340815&siteId=1