0

I am creating a flappy bird game using eclipse and my key bindings to move up or down aren't having any effect and the bird just plops to the bottom of the JFrame. I need help if maybe the syntax is correct or not, I even put addKeyListener(this); In the JFrame the oval shows up but thats it and it just sinks to the bottom

    public FlappyBird() {
        // Creating the parameters of the size of the frame with a set size
        Dimension d = new Dimension(FlappyBird.WIDTH, FlappyBird.HEIGHT);
        setSize(d);

    // The initial positions for the setting and birdie
        setting = new Setting(100); 
        birdie = new Bird(20,FlappyBird.HEIGHT/2,setting.pipes);
        addKeyListener(this);
        requestFocus();
    }


    private void updateGame() {
        // TODO Auto-generated method stub
        setting.updateGame();
        birdie.updateGame();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            birdie.isPressed = true;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        if(e.getKeyCode() == KeyEvent.VK_SPACE          ) {
            birdie.isPressed = false;
        }
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
}
 The second class is



public class Bird extends Rectangle {


    /**
     * 
     */
    // No idea what this does
    private static final long serialVersionUID = 1L;
    // Speed of the birdie
    private int speed = 4;
    // Checking if a certain key is pressed or not
    public boolean isPressed = false;
    private ArrayList <Rectangle> pipes;
    public Bird(int x, int y, ArrayList <Rectangle> pipes) {
        setBounds(x,y,32,32);
        this.pipes = pipes;
    }

    public void updateGame() {
        if(isPressed) {
            y-=speed;
        }
        else {
            y+=speed;
        }
        // If the birdie touches the pipes the window will close
        for (int k = 0; k < pipes.size(); k++) {
            if(this.intersects(pipes.get(k))) {
            System.exit(1);
        }
        }
    }
    public void renderGame(Graphics g) {
        g.setColor(Color.RED);
        g.fillOval(x, y, width, height);

    }

}
Mason
  • 1
  • Ah, `KeyListener`, poor, misunderstood and unreliable soul. Understand, any answer which doesn't suggest you use the [Key bindings API](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) instead, is an unreliable hack – MadProgrammer May 31 '18 at 06:48
  • Do you have any idea why the KeyListener with the KeyEvent isnt working? – Mason May 31 '18 at 06:50
  • Dozens, pick one. In order for the component to receive key events, it must be focusable AND have keyboard focus. You request focus in your constructor, this suggests that the component is not yet attached to a native peer, so it can possible receive focus. [`requestFocus`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#requestFocus--) is also unreliable. As I said, I can tell 100 hacks which "might" work "some" of the times, but I can tell 1 solution which will work all the time - Key Bindings – MadProgrammer May 31 '18 at 06:53
  • Is there a way to make it work by using KeyListeners and KeyEvents? – Mason May 31 '18 at 06:55
  • Not in a reliable way - I'm not "pushing" key bindings because they are a "personal" favourite, I'm "suggesting" them because it works reliably and is designed to solve this very issue - please forgive me, but this type of question gets asked 2 or 3 times a day - the repeated advice is, use the Key Bindings API - it's what is there for – MadProgrammer May 31 '18 at 06:57
  • Oh ok I see, do you know how I can make keybindings work in this game, sorry Im a noob and don't understand half the things I do – Mason May 31 '18 at 06:57
  • Well, [that's an example](https://stackoverflow.com/questions/50612426/how-to-draw-an-image-over-a-background-image/50613543#50613543) and [another](https://stackoverflow.com/questions/50539426/how-to-add-a-new-jframe-with-buttons/50539518#50539518) and [another](https://stackoverflow.com/questions/50012694/moving-a-rectangle-using-keylistener/50012873#50012873) and I linked to the tutorials in the first comment – MadProgrammer May 31 '18 at 06:59
  • thanks, by the way could my code not be working because I didnt write the Scanner for the keyboard? – Mason May 31 '18 at 07:01
  • Scanner is for console input, it won’t help you here – MadProgrammer May 31 '18 at 07:47

0 Answers0