我正在编写一个类似于打砖块的简单游戏。有必要按左箭头进行平台移动。负责点击按钮的代码:
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();
}
}
});
问题是,当您启动并按下按钮时,什么也没有发生。
游戏场.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();
左移方法
public void LeftShift(){
double dx = 10;
GameField.platform.relocate(getLayoutX()-dx, getLayoutY());
}
饮歌长啸
相关分类