2017-02-23 89 views
-1

我正在使用Javafx嘗試創建一個2D玩家,玩家(玩家等級)將能夠將一個箱子(食品類)移動到某個位置。我可以讓玩家與我想移動的圖像發生碰撞,但不會在網格上移動。我已經設置了一個system.out消息來充當一個調試器,但仍然沒有動作。對象不會碰撞

public class Game extends Application{ 
      Pane backgroundPane; 
      Pane playfieldLayer; 
      Pane scoreLayer; 

      Image playerImage; 
      Image foodImage; 

      List<Player> players = new ArrayList<>(); 
      List<Food> foods = new ArrayList<>(); 

      Text collisionText = new Text(); 
      boolean collision = false; 

      Scene scene; 

      @Override 

      public void start(Stage theStage) { 

       Group root = new Group(); 
       int columnAmount = 18; 
       int rowAmount = 18; 

       GridPane gameGrid = new GridPane(); 

       for (int i = 0; i < columnAmount; i++) { 
        ColumnConstraints columnn = new ColumnConstraints(45); 
        gameGrid.getColumnConstraints().add(columnn); 

       } 

       for (int i = 0; i < rowAmount; i++) { 
        RowConstraints row = new RowConstraints(45); 
        gameGrid.getRowConstraints().add(row); 
       } 

       gameGrid.setStyle("-fx-background-color: white; -fx-grid-lines-visible:true"); 


      root.getChildren().add(gameGrid); 
       // create layers 
       backgroundPane = new Pane(); 
       backgroundPane.setId("root"); 
       playfieldLayer = new Pane(); 
       scoreLayer = new Pane(); 

       root.getChildren().add(backgroundPane); 
       root.getChildren().add(playfieldLayer); 
       root.getChildren().add(scoreLayer); 

       scene = new Scene(root, Settings.SCENE_WIDTH, Settings.SCENE_HEIGHT); 
       backgroundPane.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm()); 

       theStage.setResizable(false); 
       theStage.setScene(scene); 
       theStage.show(); 

       loadGame(); 
       createPlayers(); 

       AnimationTimer gameLoop = new AnimationTimer() { 

        @Override 
        public void handle(long now) { 

         // player input 
         players.forEach(sprite -> sprite.processInput()); 
         spawnFood(); 

         players.forEach(sprite -> sprite.move()); 
         foods.forEach(sprite -> sprite.move()); 


         checkCollisions(); 

         players.forEach(sprite -> sprite.updateUI()); 
         foods.forEach(sprite -> sprite.updateUI()); 
        } 
       }; 
       gameLoop.start(); 
      } 

      private void loadGame() { 
       playerImage = new Image(getClass().getResource("warehouse.png").toExternalForm()); 
       //enemyImage = new Image(getClass().getResource("enemy.png").toExternalForm()); 
       foodImage = new Image(getClass().getResource("food.png").toExternalForm()); 
      } 

      private void createPlayers() { 
       // player input 
       Input input = new Input(scene); 

       // register input listeners 
       input.addListeners(); // TODO: remove listeners on game over 

       Image image = playerImage; 

       // center horizontally, position at 70% vertically 
       double x = (Settings.SCENE_WIDTH - image.getWidth())/2.0; 
       double y = Settings.SCENE_HEIGHT * 0.7; 

       // create player 
       Player player = new Player(playfieldLayer, image, x, y, 0, 0, 0, Settings.PLAYER_SPEED, input); 

       // register player 
       players.add(player); 

      } 

      Input input = new Input(scene); 

     input.addListeners(); 


     Image image = foodImage; 

      double x = (Settings.SCENE_WIDTH - image.getWidth())/2.0; 
      double y = Settings.SCENE_HEIGHT * 0.2; 

     Food food = new Food(playfieldLayer, image, x,y,0,0, Settings.PLAYER_SHIP_SPEED, input); 

     foods.add(food); 

      private void checkCollisions() { 

       collision = false; 

       for(Player player: players) { 
        for(Food food: foods) { 
         if(player.collidesWith(food)) { 
          Food.collision = true; 
          food.updateUI(); 
          System.out.println("Collided"); 
         } 
        } 
       } 
      } 


      public static void main(String[] args) { 
       launch(args); 
      } 

    } 
    public abstract class SpriteBase { 
    public void move() { 
      if(!canMove) 
       return; 

      x += dx; 
      y += dy; 
     } 

    public void updateUI() { 
      imageView.relocate(x, y); 
    } 
    } 

public class Food extends SpriteBase { 
    double foodMinX; 
    double foodMaxX; 
    double foodMinY; 
    double foodMaxY; 
    double speed; 
    Input input; 

    static boolean collision; 

public Food(Pane layer, Image image, double x, double y, double dx, double dy, double speed, Input input) { 
     super(layer, image, x, y, dx, dy); 
     this.speed = speed; 
     this.input = input; 
     checkBounds1(); 
    } 

private void checkBounds1() { 
     // calculate movement bounds of the player ship 
     // allow half of the ship to be outside of the screen 
    foodMinX = 0 - image.getWidth()/2.0; 
    foodMaxX = Settings.SCENE_WIDTH - image.getWidth()/2.0; 
    foodMinY = 0 - image.getHeight()/2.0; 
    foodMaxY = Settings.SCENE_HEIGHT -image.getHeight()/2.0; 
    } 

public void processInput() { 
    if(input.isMoveUp() && collision != false) { 
     input.removeListeners(); 
     dy = -speed; 
     collision = false; 
     System.out.println("move up you bastard"); 
    } else if(input.isMoveDown() && collision != false) { 
     input.removeListeners(); 
     System.out.println("move up you bastard"); 
     dy = speed; 
     collision = false; 
    } else { 

     dy = 0d; 
    } 

    // horizontal direction 
    if(input.isMoveLeft() && collision == true) { 
     input.removeListeners(); 
     System.out.println("move up you bastard"); 
     collision = false; 
     dx = -speed; 
    } else if(input.isMoveRight() && collision == true) { 
     input.removeListeners(); 
     System.out.println("move up you bastard"); 
     collision = false; 
     dx = speed; 
    } else { 
     dx = 0d; 
    } 

    } 


@Override 
    public void move() { 
     super.move(); 
     // ensure the food can't move outside of the screen 
     checkBounds(); 
    } 

private void checkBounds() { 

     // vertical 
     if(Double.compare(y, foodMinY) < 0) { 
      y = foodMinY; 
     } else if(Double.compare(y, foodMaxY) > 0) { 
      y = foodMaxY; 
     } 

     // horizontal 
     if(Double.compare(x, foodMinX) < 0) { 
      x = foodMinX; 
     } else if(Double.compare(x, foodMaxX) > 0) { 
      x = foodMaxX; 
     } 
    } 
+2

我懷疑你發佈的代碼比大多數人想要瀏覽的要多得多。嘗試並將其減少到[mcve] – khelwood

+0

設置食物時,您將'0'傳遞給'dx'和'dy'。所以'move()'函數被調用,它只是給你的'x'和'y'加'0'。在你的'spawnFoods'方法中。 –

+0

我減少了一些。你認爲它看起來更好嗎?還是你會建議進一步削減它? 謝謝你的回覆Hypnic Jerk,非常友善。你會建議如何解決這個問題? – callumSteven

回答

0

繼續評論。

當你調用方法spawnFood(),你是一個dx製作new Food(),和0dy值。所以它沒有移動的速度。

Food food = new Food(playfieldLayer, image, x,y,0,0); <--Here 

「但我在Player班上也這麼做!」 (編輯了)

Player player = new Player(playfieldLayer, image, x, y, 0, 0, 0, Settings.PLAYER_SPEED, input);<-- Here 

你在0通爲dx,dy當你創建Player爲好,除非你也傳遞給它Settings.PLAYER_SPEED,然後您可以在構造函數中設置。

public Player(Pane layer, Image image, double x, double y, double r, double dx, double dy, double speed, Input input) { 

    super(layer, image, x, y, dx, dy); 

    this.speed = speed; <--Here 
    this.input = input; 

    checkBounds1(); 
} 

你不這樣做在Food類。

public Food(Pane layer, Image image, double x, double y, double dx, double dy) { 
     super(layer, image, x, y, dx, dy); 
     /***** NOT HERE ****/ 
     checkBounds1(); 

    } 

我建議你spawnFood()方法改變dx,dy值,別的東西,或者也可以通過它的Settings.PLAYER_SPEED,因爲我想你要的食物留在字符的前面。

+0

非常感謝你的迴應。我已經提前嘗試輸入您的建議。基本上我改變了我的遊戲類中的spawnFood()和checkCollision()方法,添加了一個輸入監聽器並傳遞了播放器的速度並添加了一個靜態布爾變量。此外,在我的食物類中,我添加了碰撞檢測,以根據玩家輸入移動圖像(我認爲這對我所需要的是相當明智的)。 但我得到這個奇怪的問題,原始圖像留在它的重生點,我只能移動圖像的方式,直到它碰到屏幕邊緣。 – callumSteven

+0

我應該提到我所做的更改是在原始帖子中 – callumSteven