Reputation: 33
I'm writing a simple game similar to Arkanoid. It is necessary to make a platform shift by pressing the left arrow. The code responsible for clicking a button:
canvas.requestFocus();
canvas.setOnKeyTyped(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.LEFT) {
System.out.println("Leffft");
platform.LeftShift();
event.consume();
}
}
});
The problem is that when you start and press the button nothing happens.
GameField.java
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.util.Duration;
public class GameField extends Application {
public static GameField me;
public GameField()
{
me=this;
}
static Pane canvas = new Pane();
static Ball ball = new Ball(5, Color.WHITE);
static Platform platform = new Platform(80,5, Color.WHITE);
Scene scene = new Scene(canvas,550,500, Color.BLACK);
static Timeline timeline;
@Override
public void start(Stage stage) {
canvas.getChildren().add(ball);
canvas.getChildren().add(platform);
ball.relocate(50, 50);
platform.relocate(235,495);
stage.setScene(scene);
stage.setTitle("Cat Arcanoid");
stage.show();
timeline = new Timeline(new KeyFrame(Duration.millis(20),
new EventHandler<ActionEvent>() {
double dx = 10;
double dy = 10;
@Override
public void handle(ActionEvent t) {
ball.setLayoutX(ball.getLayoutX() + dx);
ball.setLayoutY(ball.getLayoutY() + dy);
dx = ball.XCollision(dx);
dy = ball.YCollision(dy);
dy = platform.PlatformCollision(dy);
canvas.requestFocus();
canvas.setOnKeyTyped(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.LEFT) {
System.out.println("Leffft");
platform.LeftShift();
event.consume();
}
}
});
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}}
LeftShift method
public void LeftShift(){
double dx = 10;
GameField.platform.relocate(getLayoutX()-dx, getLayoutY());
}
Upvotes: 1
Views: 608
Reputation: 632
The key events provide two 'levels' of event.
The higher level (from keyTyped)
"Key typed" events are higher-level and generally do not depend on the platform or keyboard layout. They are generated when a Unicode character is entered, and are the preferred way to find out about character input. ................. No key typed events are generated for keys that don't generate Unicode characters (e.g., action keys, modifier keys, etc.).
The lower level 'keyPressed' and 'keyReleased' events might give you what you want:
"Key pressed" and "key released" events are lower-level and depend on the platform and keyboard layout. They are generated whenever a key is pressed or released, and are the only way to find out about keys that don't generate character input (e.g., action keys, modifier keys, etc.). The key being pressed or released is indicated by the code variable, which contains a virtual key code.
Upvotes: 1