2017-02-23 10 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

になります。食べ物をセットアップするとき、あなたは 'dx'と' dy'に '0'を渡しています。 'move()'関数が呼び出されると、 'x'と' y'に '0'が追加されます。あなたの 'spawnFoods'メソッドで。 –

+0

私は幾分それを減らしました。あなたはそれがより良く見えると思いますか、それをさらに減らすことを提案しますか? レスポンスありがとうございますHypnic Jerk、非常に親切です。あなたはそれを修正するために何を提案しますか? – callumSteven

答えて

0

コメントから続きます。あなたがメソッドを呼び出すとspawnFood()

、あなたはdxnew Food()を作り、そして0dy値がされています。したがって、移動する速度はありません。

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

"でも、私はPlayerクラスで同じことをしています!"あなたにもPlayerを作成するときにも、それSettings.PLAYER_SPEED渡し除いあなたはその後、コンストラクタで設定され、dx,dyため0に渡すん

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

(アウト編集しました)。

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