xml
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.effect.*?> 4<?import javafx.scene.control.*?> 5<?import javafx.scene.text.*?> 6<?import javafx.scene.shape.*?> 7<?import java.lang.*?> 8<?import javafx.scene.layout.*?> 9 10<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#KeyPress" onKeyReleased="#KeyRelease" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Game"> 11 <children> 12 <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#838383" height="400.0" layoutX="425.0" stroke="#838383" strokeType="INSIDE" width="175.0" /> 13 <Rectangle arcHeight="5.0" arcWidth="5.0" height="400.0" stroke="BLACK" strokeType="INSIDE" width="425.0" /> 14 <Label layoutX="486.0" layoutY="14.0" prefHeight="43.0" prefWidth="78.0" text="Score" textFill="#1b92e8"> 15 <font> 16 <Font name="Lao UI Bold" size="29.0" /> 17 </font> 18 <effect> 19 <Bloom /> 20 </effect> 21 </Label> 22 </children> 23</AnchorPane>
java
1import javafx.scene.input.KeyEvent; 2import javafx.scene.layout.AnchorPane; 3 4public class Game implements Runnable{ 5 6//キー入力 7 int[] KEY = new int[3]; 8 public void KeyPress(KeyEvent e){ 9 keys(e,1); 10 } 11 public void KeyRelease(KeyEvent e){ 12 keys(e,0); 13 } 14 15 private void keys(KeyEvent e,int value){ 16String key = e.getCode().getName(); 17 18 if(key.equals("Right")){ 19 KEY[0] = value; 20 }else if(key.equals("Left")){ 21 KEY[1] = value; 22 }else if(key.equals("Space")){ 23 KEY[2] = value; 24 } 25 } 26 27 28 AnchorPane root; 29 Player player; 30 31 public void GStart(AnchorPane pane){ 32 33 root = pane; 34 player = new Player(); 35 root.getChildren().add(player); 36 37 Thread th = new Thread(this); 38 th.setDaemon(true); 39 th.start(); 40 41 } 42 43 44 @Override 45 public void run() { 46 47 do{ 48 action(); 49 try {Thread.sleep(1000/10);} catch (InterruptedException e){} 50 51 }while(true); 52 53 } 54 55 private void action(){ 56 System.out.println(KEY[0]); 57 58 if(KEY[0] == 1){ 59 double x =player.getX(); 60 x += 1; 61 player.setX(x); 62 }else if(KEY[1] == 1){ 63 double x =player.getX(); 64 x -= 1; 65 player.setX(x); 66 } 67 68 } 69 70} 71
java
1 2import javafx.application.Application; 3import javafx.fxml.FXMLLoader; 4import javafx.scene.Scene; 5import javafx.scene.layout.AnchorPane; 6import javafx.stage.Stage; 7 8public class Main extends Application{ 9 10 public static void main(String[] args) { 11 launch(args); 12 } 13 14 @Override 15 public void start(Stage stage) throws Exception { 16 17 AnchorPane pane = FXMLLoader.load(getClass().getResource("/Frame.fxml")); 18 Scene s = new Scene(pane); 19 pane.requestFocus(); 20 21 Game g = new Game 22 g.GStart(pane); 23 24 stage.setScene(s); 25 stage.show(); 26 27 28 } 29 30}
JavaFXを使ってゲームを作っているのですが、キーボード入力がうまくいきません。
KeyPress()メソッドで変数が変更されているのにrun()メソッドでは変更されていません。
できるだけ詳しく教えてください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/29 14:26
2016/04/29 14:41 編集
2016/04/30 01:33 編集
2016/04/30 02:41
2016/04/30 03:18