2013-10-26 3 views
0

응용 프로그램에 문제가 있습니다. 내 xAxis 변수를 입력하기 위해 응용 프로그램을 열면 내 "터미널"에 집중해야합니다.JavaFX Stage가있는 스캐너를 사용하여 키보드에서 입력 받기

무대에 초점을 맞추고 싶을 때도 변수를 입력 할 수 있습니다. http://i.imgur.com/hKqMjoa.png는 또한

세력 "응답하지"몇 가지 도움이 (내가 스크린 샷을 게시 할 수있는 충분한 평판이없는) :

문제의 스크린 샷을 (내가 강조 내가 집중해야 할 곳이다) 도움이되지만, 그 정도의 프로그램에는 영향을 미치지 않습니다. 당신이 수익을 누를 때까지

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package gamefx; 

import java.util.Scanner; 
import javafx.animation.Animation; 
import javafx.animation.AnimationTimer; 
import javafx.animation.TranslateTransition; 
import javafx.application.Application; 
import javafx.geometry.Rectangle2D; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class GameFX extends Application { 

public Image img = new Image("Block1.png"); 
public ImageView image = new ImageView(img); 
public int yAxis = -300; 
public int xAxis = 0; 
public TranslateTransition tt = new TranslateTransition(Duration.millis(5000), image); 
public Scanner sn = new Scanner(System.in); 
public String ifelse; 

public void spriteAnimation(){ 
tt.setByY(yAxis); 
tt.setCycleCount(1); 
tt.play(); 
} 
public void getMove(){ 

ifelse = sn.nextLine(); 
switch (ifelse) { 
    case "d": 
     rightMove(); 
     System.out.print("Move Right"); 
     break; 
    case "a": 
     leftMove(); 
     System.out.print("Move left"); 
     break; 
} 

} 

public int leftMove(){ 
xAxis = -100; 
return xAxis; 

} 
public int rightMove(){ 
xAxis = 100; 
return xAxis; 
} 
@Override 
public void start(Stage primaryStage) throws InterruptedException{ 
    primaryStage.setTitle("Game"); 

    StackPane stckp1 = new StackPane(); 
    Scene scn = new Scene(stckp1, 700, 700); 

    primaryStage.show(); 
    getMove(); 
    stckp1.getChildren().add(image); 
    image.setTranslateX(xAxis); 
    spriteAnimation(); 
    primaryStage.setScene(scn); 



} 

} 

답변

1

scanner.nextLine()에 대한 호출은 전체 응용 프로그램을 차단합니다 다음과 같이

코드입니다. 자바 FX와 같은

위젯 라이브러리를 처리 키보드 입력을 event handling mechanisms을 제공

scn.setOnKeyPressed(new EventHandler<KeyEvent>() { 
     @Override 
     public void handle(KeyEvent event) { 
      if (event.getCode() == KeyCode.D) { 
       System.out.print("Move Right"); 
      } else if (event.getCode() == KeyCode.A) { 
       System.out.print("Move left"); 
      } 
      event.consume(); 
     } 
    }); 
+0

감사합니다, 좀 더 연구를해야, 나는 이벤트 핸들러를 사용하여 생각하지 않았다. – DRH1469