2017-04-05 45 views
1

我实际上学习JavaFX并试图做出一个简单的应用 用箭头正确地移动一个字符的图片。JavaFX刷新重新绘制一个新的位置的图片

我做了onKeyPressed部分这是工作,我可以修改对象的变量locationX和locationY,但图像不是mooving,有没有 有点像“repaint()”方法来做到这一点干脆?或者我必须使用动画?我宁愿暂时不使用动画。

Fenetre.java

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.layout.HBox; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class Fenetre extends Application{ 

    public Charac c1; 

    public void init(){ 
     c1 = new Charac(110,110); 
    } 

    public void start(Stage stage){ 
     ImageView ivChar = new ImageView(); 
     ivChar.setImage(c1.getImg()); 
     ivChar.relocate((double)c1.getLocationX(),(double)c1.getLocationY()); 

     HBox box = new HBox(); 
     box.setTranslateX((double)c1.getLocationX()); 
     box.setTranslateY((double)c1.getLocationY()); 
     box.getChildren().add(ivChar); 
     box.setFocusTraversable(true); 
     box.setOnKeyPressed(new EventHandler<KeyEvent>(){ 
      @Override 
      public void handle(KeyEvent e) { 
       switch(e.getCode()){ 
        case UP: 
         c1.goUp(); 
         break; 
        case DOWN: 
         c1.goDown(); 
         break; 
        case LEFT: 
         c1.goLeft(); 
         break; 
        case RIGHT: 
         c1.goRight(); 
         break; 
       default: 
        break; 
       } 
      } 
     }); 

     Group root = new Group(); 
     root.getChildren().add(box); 
     Scene scene = new Scene(root); 
     scene.setFill(Color.BLACK); 


     stage.setWidth(600); 
     stage.setHeight(600); 
     stage.setTitle("BBMAN"); 
     stage.setScene(scene); 
     stage.show(); 


    } 
} 

Charac.java

import javafx.scene.Parent; 
import javafx.scene.image.Image; 

public class Charac extends Parent{ 
    private int locationX=0; 
    private int locationY=0; 
    private Image img = new Image("/Images/bbfront.png"); 

    public Charac(int locationX, int locationY){ 
     this.locationY=locationY; 
     this.locationX=locationX; 


    } 


    public void setLocationX(int locationX){ 
     this.locationX=locationX; 
    } 

    public int getLocationX(){ 
     return this.locationX; 
    } 

    public int getLocationY(){ 
     return this.locationY; 
    } 
    public void setLocationY(int locationY){ 
     this.locationY=locationY; 
    } 

    public void goRight(){ 
     this.locationX+=1; 
     this.img = new Image("/Images/bbright.png"); 
    } 

    public void goLeft() { 
     this.locationX-=1; 
     this.img = new Image("/Images/bbleft.png"); 
    } 

    public void goUp(){ 
     this.locationY-=1; 
     this.img = new Image("/Images/bbup.png"); 
    } 

    public void goDown(){ 
     this.locationY+=1; 
     this.img = new Image("/Images/bbfront.png"); 

    } 

    public Image getImg(){ 
     return this.img; 
    } 
} 

我不知道RLY如果我在正确的部分我张贴,这是我的第一篇文章。

谢谢大家!

+0

按下按钮后,您不会更新HBox的位置。 – Steven

+0

相关,也许是重复:[如何编写JavaFX的KeyListener](http://stackoverflow.com/questions/29962395/how-to-write-a-keylistener-for-javafx),它围绕屏幕上的按键。 – jewelsea

回答

2

下面的代码已经过测试,虽然使用不同的图形。 (我必须重做加载才能使用我的Eclipse文件结构。)

可以使用“根”节点作为键盘事件处理程序。主要的是要记住,使它FocusTraversable并给它的焦点。

我剥离了一些不需要的Charac中的一些代码,并将图形预加载到一个数组中,因此结构将更像一个精灵,因此您不会加载新图形(相对昂贵)每次你改变方向。

有一些冗余代码(三行)初始化ImageView,然后在KeyEvent处理例程中对其进行更新。可能可以而且应该被淘汰。

最主要的是您可以直接设置ImageView的X和Y,并更新ImageView显示的图像。我发现ImageView是一个非常方便的类,因此我经常使用它。改变不透明度和缩放比例的能力也非常好。

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class CharacMove extends Application 
{ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception 
    { 
     Group root = new Group(); 
     Scene scene = new Scene(root); 

     scene.setFill(Color.BLACK); 
     stage.setWidth(600); 
     stage.setHeight(600); 
     stage.setTitle("BBMAN"); 
     stage.setScene(scene); 

     Charac c1 = new Charac(); 
     ImageView ivChar = new ImageView(); 
     ivChar.setImage(c1.getImage()); 
     ivChar.setX(c1.getX()); 
     ivChar.setY(c1.getY()); 

     root.setFocusTraversable(true); 
     root.requestFocus(); 
     root.setOnKeyPressed(new EventHandler<KeyEvent>() 
     { 
      @Override 
      public void handle(KeyEvent e) 
      { 
       switch(e.getCode()) 
       { 
        case UP: 
         c1.goUp(); 
         break; 
        case DOWN: 
         c1.goDown(); 
         break; 
        case LEFT: 
         c1.goLeft(); 
         break; 
        case RIGHT: 
         c1.goRight(); 
         break; 
        default: 
         break; 
       } 
       ivChar.setImage(c1.getImage()); 
       ivChar.setX(c1.getX()); 
       ivChar.setY(c1.getY()); 
      } 
     }); 

     root.getChildren().add(ivChar); 
     stage.show(); 
    } 

    class Charac 
    { 
     private Image[] img; 
     private double xLoc, yLoc; 
     private int currentImg;  

     public Image getImage() { return img[currentImg]; } 
     public double getX() { return xLoc; } 
     public double getY() { return yLoc; } 

     public Charac() 
     { 
      img = new Image[4]; 
      img[0] = new Image(getClass().getResource("Images/bbright.png").toString()); 
      img[1] = new Image(getClass().getResource("Images/bbleft.png").toString()); 
      img[2] = new Image(getClass().getResource("Images/bbup.png").toString()); 
      img[3] = new Image(getClass().getResource("Images/bbfront.png").toString()); 
     } 

     public void goRight() 
     { 
      xLoc += 1; 
      currentImg = 0; 
     } 

     public void goLeft() 
     { 
      xLoc -= 1; 
      currentImg = 1; 
     } 

     public void goUp() 
     { 
      yLoc -= 1; 
      currentImg = 2; 
     } 

     public void goDown() 
     { 
      yLoc += 1; 
      currentImg = 3; 
     } 
    } 
} 

当我使用JavaFX开始,我写了并且张贴在java-gaming.org一个简单的游戏动画教程,那需要的东西另一对夫妇的步骤,例如,使用AnimationTimer,如果/当你准备好探索一下。你可以看看here。但那时我仍然没有想到我可以简单地使用Group node root来进行键盘处理。

1

按下键后,您需要更新HBox box的位置。

box.setOnKeyPressed(new EventHandler<KeyEvent>(){ 
     @Override 
     public void handle(KeyEvent e) { 
      switch(e.getCode()){ 
       case UP: 
        c1.goUp(); 
        break; 
       case DOWN: 
        c1.goDown(); 
        break; 
       case LEFT: 
        c1.goLeft(); 
        break; 
       case RIGHT: 
        c1.goRight(); 
        break; 
      default: 
       break; 
      } 
      box.setTranslateY((double)c1.getLocationY()); 
      box.setTranslateX((double)c1.getLocationX()); 
     } 
    }); 

也可能最好,如果您将图像文件加载到数组。