我正在编写一个类似于打砖块的简单游戏。有必要按左箭头进行平台移动。负责点击按钮的代码: 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(); } } });问题是,当您启动并按下按钮时,什么也没有发生。游戏场.javaimport 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;@Overridepublic 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());}
1 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
关键事件提供了两个“级别”的事件。
更高级别(来自 keyTyped)
“按键类型”事件是更高级别的,通常不依赖于平台或键盘布局。它们在输入 Unicode 字符时生成,是查找字符输入的首选方式。................... 不会为不生成 Unicode 字符的键(例如,操作键、修饰键等)生成键类型事件。
较低级别的 'keyPressed' 和 'keyReleased' 事件可能会给你你想要的:
“按键按下”和“按键释放”事件是较低级别的,取决于平台和键盘布局。每当按下或释放键时都会生成它们,并且是找出不生成字符输入的键(例如,操作键、修饰键等)的唯一方法。被按下或释放的键由代码变量指示,该变量包含一个虚拟键代码。
添加回答
举报
0/150
提交
取消