Reputation: 9
I have tried using a KeyListener to make the character rise which just simply didnt do anything. Is there a better way to do this? If not, does anyone know why the KeyListener isn't working? I would really appreciate some help. Also, I call this method in a Timeline so the bird does not just fall 1 pixel and will not rise by one either
Here is the code for the KeyListener (The bird falling part works fine by the way):
public static void birdJump() {
birdView.relocate(100, fall++);
if(fall > 201)
birdView.setRotate(50);
FlappyBird.root.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode().equals(32)) {
birdView.relocate(100, fall--);
if(fall > 199)
birdView.setRotate(-50);
}
}
});
}
Thanks!
Upvotes: 0
Views: 605
Reputation: 1776
In addition to what James_D said, I think it's because you use the KeyEvent
on a Layout that does not have the focus by default since it belongs to the Scene
, to remedy it you have to give it the focus :
root.setFocusTransversale(true); /* Enable focus on the layout */
root.requestFocus(); /* give the focus to the layout */
I hope this will solve your problem !
Upvotes: 1
Reputation: 209330
Read the documentation: event.getCode()
does not return an int
(or a number of any kind), so event.getCode().equals(32)
cannot possibly be true
.
You want
if (event.getKeyCode() == KeyCode.SPACE) {
// ...
}
Upvotes: 2