0

Is there any way in Java (Eclipse) to move a player object that is defined as an object on a matrix tile ?

So, the start position of the player object is (0,0). The playboard is a 12x12 matrix. The existing code that I have written works fine to create the player object (as mentioned, as an object on a matrix tile whereby the matrix is defined as [row,col] ).

I am looking to a method to start moving the player based on certain conditions. e.g. a button (I know how to create). So, the question is really how to start moving the player defined as an object on a matrix tile (PlayerPion in the code below).

Thanks in advance for your responses ;).

  GamePanel()   {


            setLimits(BorderFactory.createLineBorder(Color.BLACK, GAP));
            GridLayout layout = new GridLayout(GamePanel.getBoardRows(), 
            GamePanel.getBoardCols());
            setLayout(layout);

            for (int i = 0; i <PlayBoard.getBoardRows(); i++)   {

                for (int j = 0; j < PlayBoard.getBoardCols(); j++)  {
                    int[][] matrixBoard = Board.getBoard();
                    int boardValue = matrixBoard[i][j];
                    switch(boardValue) {
                        case AA: add(new Tile(Color.WHITE)); 
                                add(new PlayerPion());
                                break; 

(Code that is not relevant is not shown)

A sample of the playBoard (output from existing code): enter image description here

bram
  • 47
  • 6
  • I recommend this answer: https://stackoverflow.com/questions/2510159/can-i-add-a-component-to-a-specific-grid-cell-when-a-gridlayout-is-used It explains that you can't directly select cells in a grid layout but that you can put panels in them as a workaround. And has example code for that. It should work well for your problem. – ProbablyStuck Dec 08 '18 at 09:20
  • @ProbablyStuck: Yes, but that I have already done as you can see in the code and the output figure ? The question is how I can, starting from that, can start moving the object that is indirectly added to the grid cells? – bram Dec 08 '18 at 09:25
  • Right, but what you have at the moment is a panel containing a gridlayout that you want to put things in right? And you have done that at the moment but you can't do player movement because you can't choose the cell to edit. What that answer shows is how to add a panel to each cell so that you can directly say something like panel[1][2].add(player) to create an instance of the player there; then you just remove the old one from its panel. – ProbablyStuck Dec 08 '18 at 09:53

0 Answers0