実現したいこと
javafxで、画面移動後、短形オブジェクトをキーコード(KeyCode)識別で「wasd」移動ができるようにしたいです。
発生している問題・分からないこと
「wasd」入力しても短形が移動しない状態となっています。
エラーメッセージ
error
1起動後のメッセージです。 2WARNING: A restricted method in java.lang.System has been called 3WARNING: java.lang.System::load has been called by com.sun.glass.utils.NativeLibLoader in module javafx.graphics (file:/E:/pleiades/javafx-sdk-24.0.2/lib/javafx.graphics.jar) 4WARNING: Use --enable-native-access=javafx.graphics to avoid a warning for callers in this module 5WARNING: Restricted methods will be blocked in a future release unless native access is enabled 6 7WARNING: A terminally deprecated method in sun.misc.Unsafe has been called 8WARNING: sun.misc.Unsafe::allocateMemory has been called by com.sun.marlin.OffHeapArray (file:/E:/pleiades/javafx-sdk-24.0.2/lib/javafx.graphics.jar) 9WARNING: Please consider reporting this to the maintainers of class com.sun.marlin.OffHeapArray 10WARNING: sun.misc.Unsafe::allocateMemory will be removed in a future release
該当のソースコード
Character:短形移動用のコードを入力しています
1package application; 2 3import java.net.URL; 4import java.util.ResourceBundle; 5 6import javafx.animation.AnimationTimer; 7import javafx.beans.binding.BooleanBinding; 8import javafx.beans.property.BooleanProperty; 9import javafx.beans.property.SimpleBooleanProperty; 10import javafx.fxml.FXML; 11import javafx.fxml.Initializable; 12import javafx.scene.input.KeyCode; 13import javafx.scene.layout.AnchorPane; 14import javafx.scene.shape.Rectangle; 15 16public class Character implements Initializable { 17 18 //キー押下変数 19 private BooleanProperty wPressed = new SimpleBooleanProperty(); 20 private BooleanProperty aPressed = new SimpleBooleanProperty(); 21 private BooleanProperty sPressed = new SimpleBooleanProperty(); 22 private BooleanProperty dPressed = new SimpleBooleanProperty(); 23 24 private BooleanBinding keyPressed = wPressed.or(aPressed).or(sPressed).or(dPressed); 25 26 private int movementVariable = 2; 27 28 @FXML 29 private AnchorPane screen; 30 31 @FXML 32 private Rectangle shape1; 33 34 AnimationTimer timer = new AnimationTimer() { 35 @Override 36 public void handle(long timestamp) { 37 38 if(wPressed.get()) { 39 shape1.setLayoutY(shape1.getLayoutY() - movementVariable); 40 } 41 42 if(sPressed.get()){ 43 shape1.setLayoutY(shape1.getLayoutY() + movementVariable); 44 } 45 46 if(aPressed.get()){ 47 shape1.setLayoutX(shape1.getLayoutX() - movementVariable); 48 } 49 50 if(dPressed.get()){ 51 shape1.setLayoutX(shape1.getLayoutX() + movementVariable); 52 } 53 54 } 55 }; 56 57 @Override 58 public void initialize(URL url, ResourceBundle resourceBundle) { 59 movementSetup(); 60 61 keyPressed.addListener(((observableValue, aBoolean, t1) -> { 62 if(!aBoolean){ 63 timer.start(); 64 } else { 65 timer.stop(); 66 } 67 })); 68 } 69 70 public void movementSetup(){ 71 screen.setOnKeyPressed(e -> { 72 if(e.getCode() == KeyCode.W) { 73 wPressed.set(true); 74 } 75 76 if(e.getCode() == KeyCode.A) { 77 aPressed.set(true); 78 } 79 80 if(e.getCode() == KeyCode.S) { 81 sPressed.set(true); 82 } 83 84 if(e.getCode() == KeyCode.D) { 85 dPressed.set(true); 86 } 87 }); 88 89 screen.setOnKeyReleased(e ->{ 90 if(e.getCode() == KeyCode.W) { 91 wPressed.set(false); 92 } 93 94 if(e.getCode() == KeyCode.A) { 95 aPressed.set(false); 96 } 97 98 if(e.getCode() == KeyCode.S) { 99 sPressed.set(false); 100 } 101 102 if(e.getCode() == KeyCode.D) { 103 dPressed.set(false); 104 } 105 }); 106 }
GameController:画面移動を格納していくクラスになっています
1import javafx.event.ActionEvent; 2import javafx.fxml.FXML; 3import javafx.fxml.FXMLLoader; 4import javafx.scene.Parent; 5import javafx.scene.Scene; 6import javafx.scene.control.Button; 7import javafx.stage.Stage; 8 9/** 10 * 画面遷移 11 * <pre> 12 * 13 * </pre> 14 */ 15public class GameController{ 16 17 //終了ボタン 18 @FXML 19 private Button end_Button; 20 //開始ボタン 21 @FXML 22 private Button start_Button; 23 24 //ステージ変数 25 Stage stage = new Stage();; 26 27 //終了ボタン押下時 28 @FXML 29 void OnClickedend_Button(ActionEvent event) { 30 end_Button.getScene().getWindow().hide();//画面遷移 31 } 32 //開始ボタン押下時 game_screenに移動 33 @FXML 34 void OnClickedstart_Button(ActionEvent event) throws IOException { 35 start_Button.getScene().getWindow().hide();//画面遷移 36 Parent screen = FXMLLoader.load(getClass().getResource("2 screen.fxml")); 37 stage.setScene(new Scene(screen)); 38 stage.show(); 39 40 } 41}
2
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.layout.AnchorPane?> 4<?import javafx.scene.shape.Rectangle?> 5 6<AnchorPane fx:id="screen" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/24.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Character"> 7 <children> 8 <Rectangle fx:id="shape1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="100.0" layoutX="450.0" layoutY="400.0" stroke="BLACK" strokeType="INSIDE" width="100.0" /> 9 </children> 10</AnchorPane>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
https://www.youtube.com/watch?v=XJhAYkRoLtY&t=204s
上記の動画のコードを使用しております。
また、使用しているライブラリーは写真のとおりとなっています。
補足
特になし

回答1件
あなたの回答
tips
プレビュー