2016-12-30 15 views
0

javafx를 사용하여 간단한 RPG 게임을 만들려고합니다. 지금까지, 나는 화면에 나타나고, 움직이고, 장애물에 부딪치지 않을 나의 성격을 얻었다. 나는 그리드 역할을하는 보드 클래스를 사용하여이 작업을 수행했습니다Javafx가 게임 캐릭터를 따름

public class TestBoard extends Pane{ 

    public static final int TILE_SIZE = 40; 
    public static final int X_DIM_TILES = 10; 
    public static final int Y_DIM_TILES = 10; 
    private Image TREE = new Image(Game.class.getResource("tree.png").toString()); 
    private Image GRASS = new Image(Game.class.getResource("grasssquare.png").toString()); 
    private Character character; 
    private GameTile[][] tiles = new GameTile[X_DIM_TILES][Y_DIM_TILES]; 
    public TestBoard(){ 

     this.setPrefWidth(TILE_SIZE * X_DIM_TILES); 
     this.setPrefHeight(TILE_SIZE * Y_DIM_TILES); 
     setUpScreen(); 
     character = new Character(this); 
     addCharacter(); 
    } 
    public boolean checkTile(double x, double y){ 
     return (x < X_DIM_TILES && x >= 0 && y < Y_DIM_TILES && y >= 0 && tiles[(int)x][(int)y].getPassable()); 
    } 
    public Character getCharacter(){ 
     return character; 
    } 
    public void addCharacter(){ 
     character.setLocation(4, 4); 

    } 
    public GameTile getTile(int x, int y){ 
     return tiles[x][y]; 
    } 
    public void setUpScreen(){ 
     for (int i = 0; i < X_DIM_TILES; i++){ 
      for (int j = 0; j < Y_DIM_TILES; j++){ 
       addTile(i, j, GRASS, true); 
      } 
     } 
     for (int j = 1; j < Y_DIM_TILES-1; j++){ 
      addTile(1, j, TREE, false); 
     } 
    } 
    public void addTile(int xCord, int yCord, Image image, boolean passable){ 
     SceneryTile tile = new SceneryTile(this, image, passable); 
     tile.setLocation(xCord, yCord); 
     tiles[xCord][yCord] = tile; 
    } 
} 

기본적으로,이 클래스는 GameTiles의 컬렉션을 보유하고 문자 개체가 있습니다. 그것은 잘 설정된 방식으로 작동하지만, 실제 RPG처럼 전체 화면을 보여주지 않고 내 주인공 만 표시하도록합니다. 현재 전체 화면이 나타납니다. 내 캐릭터와 함께 화면을 움직이고 전체 화면을 드러내지 못하게하고 싶습니다. 더 많은 문서가 필요한 경우

public void start(Stage primaryStage) throws Exception { 
    board = new TestBoard(); 
    testManager = new TestManager(this, board); 
    Scene scene = new Scene(board); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
    setUpAnimation(); 
    setUpKeyPresses(); 
} 
How the screen appears

은 알려 주시기 바랍니다! 감사.

+0

그냥 약간의 조언 java.lang 패키지의 Java 표준 클래스를 사용합니다. 캐릭터가 그 중 하나입니다. – Mordechai

+0

[JavaFX에서 타일 엔진 작성하기] (https://www.javacodegeeks.com/2013/01/writing-a-tile-engine-in-javafx.html) (슈퍼 짜증나는 팝업을주의하십시오) 및 [FXGameEngine] (https://github.com/eppleton/FXGameEngine). – jewelsea

+0

@ jewelsea 내가 이것을 더 빨리 보았 더라면 좋겠다. 고맙습니다! – user7356447

답변

1

전체 레벨을 동시에로드하려면 배경 및 문자를 Group에 넣을 수 있습니다. 캐릭터가 움직일 때마다, 번역 속성을 조정합니다. 전체 그룹의 translate 속성도 조정되지만 반대 방향으로 조정됩니다. Group이 관리되지 않는지 확인하십시오. 그렇지 않으면 부모는 자신의 크기를 결정할 때 크기를 고려합니다.

다음은 간단한 예제 만 이동할 수 있도록 보여줍니다 왼쪽 문자로 원과 배경으로 그라데이션으로 채워진 사각형 오른쪽 : 동일 이름을 선택하지 마십시오

@Override 
public void start(Stage primaryStage) { 
    Circle character = new Circle(100, 100, 20, Color.RED); 
    Rectangle background = new Rectangle(2000, 200, new LinearGradient(0, 0, 100, 0, false, CycleMethod.REPEAT, new Stop(0, Color.WHITE), new Stop(100, Color.BLUE))); 
    Group group = new Group(background, character); 
    group.setManaged(false); 

    Pane root = new Pane(group); 
    root.setPrefSize(200, 200); 

    Scene scene = new Scene(root); 
    scene.setOnKeyPressed(evt -> { 
     double direction = -1; 
     switch (evt.getCode()) { 
      case RIGHT: 
       direction = 1; 
      case LEFT: 
       double delta = direction * 10; 
       character.setTranslateX(character.getTranslateX() + delta); 
       group.setTranslateX(group.getTranslateX() - delta); 
     } 
    }); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
+0

그게 효과가! 정말 고마워. 나는 내 프로그램의 조직이 이것을 사용하여 다소 망쳐 놓은 것 같다고 생각한다. 게시판을 그룹으로 묶어서 번역 할 수있게했지만 움직일 때마다 캐릭터 애니메이션이 훨씬 뒤쳐져 있습니다. – user7356447

+0

@ user7356447 전체 맵을로드하는 경우 이는 많은 노드를 최신 상태로 유지해야 할 필요가 있습니다. 표시 할 필요가없는 노드는 제거 할 수 있으므로 뷰 외부에있는 한 무시할 수 있습니다. – fabian