简体   繁体   English

Java 2D数组,可以设置单个大小吗?

[英]Java 2D array, possible to set individual size?

Quick question about Java 2D arrays; 有关Java 2D数组的快速问题; For my tile-based, top-down, 2D game (using swing) I use a 2D array to create a map, like this 对于我的基于图块的,自上而下的2D游戏(使用秋千),我使用2D数组创建地图,如下所示

public int[][] createMap(){
    return new int[][]{
    {0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}};
}

I then use this in my gameComponents class where I draw the individual tiles unto the map, like this 然后,在我的gameComponents类中使用它,在其中将单个图块绘制到地图上,如下所示

protected void paintComponent(Graphics g){
  super.paintComponent(g);

  for (int row = 0; row < game.getMap().getWidth(); row++) {
      for (int col = 0; col < game.getMap().getHeight(); col++) {
         g.drawImage(tile.getTileImage().get(values()[game.getMap().getMapArray()[col][row]]), row * SIZE,
            col * SIZE, this);
    }

} }

(where size is the size of a tile) (其中size是图块的大小)
This works, and it correctly draws each tile to the map as expected, however this also causes a problem for collision detection. 这可以正常工作,并且可以按预期正确地将每个图块绘制到地图上,但是这也会导致碰撞检测问题。 As you may have noted, while I do define the size between the tiles in draw method, it is not defined in the array at all. 您可能已经注意到,虽然我确实在draw方法中定义了图块之间的大小,但它根本没有在数组中定义。 Which, as you'd imagine, raises issues when checking for collision as the drawn tile is not where the tile is in the 2D array (due to size offset). 就像您想象的那样,这在检查碰撞时会产生问题,因为绘制的图块不在2D数组中(由于尺寸偏移)。

This is the code I use for checking collision (of course, not working due to ArrayIndexOutofbounds). 这是我用于检查冲突的代码(当然,由于ArrayIndexOutofbounds而无法正常工作)。

    public boolean collisionDetected(int xDirection, int yDirection, Game game, Player player){
      for (int row = 0; row < game.getMap().getHeight() * 16; row ++){
        for (int col = 0; col < game.getMap().getWidth() * 16; col++) {
          System.out.println(col + xDirection + player.getPositionX());
          if(game.getMap().getTile(col + xDirection + player.getPositionX() ,
               row + yDirection + player.getPositionY()) == Tiles.GRASS ){
            System.out.println("COLLISION DETECTED");
            return true;
    }
    }
}

return false;
}

This method uses a method within the map class that returns the tile on that specific coordinate, like this 此方法使用map类中的一种方法,该方法返回该特定坐标上的图块,如下所示

public Tiles getTile(int col,int row){
    return Tiles.values()[mapArray[col][row]];
}

And, of course, as the 2D array doesn't know of the size offset, it just throws an arrayindexoutofbound. 而且,当然,由于2D数组不知道大小偏移量,因此只会抛出arrayindexoutofbound。
My question is, is it possible to define a 2D map array with the size of a tile in-mind? 我的问题是,是否有可能考虑瓷砖的大小来定义2D地图数组? I appreciate any help & input I can get, after-all I am here to learn! 感谢您能获得的任何帮助和投入,毕竟我是来这里学习的!

Extra clarification: All the tiles are in an enum class (ie AIR, GRASS, STONE...). 额外说明:所有磁贴都属于枚举类(即AIR,GRASS,STONE ...)。 Also worth noting that the player position is not bound by an array, I merely move it the amount of pixels I want it to move. 同样值得注意的是,玩家的位置不受数组的限制,我只是将其移动想要移动的像素数量。

Thanks in advance! 提前致谢!

This method uses a method within the map class that returns the tile on that specific coordinate, like this 此方法使用map类中的一种方法,该方法返回该特定坐标上的图块,如下所示

public Tiles getTile(int col,int row){
    return Tiles.values()[mapArray[col][row]];
}

So if you have a "coordinate", why do you call the parameters col/row? 因此,如果您有一个“坐标”,为什么还要调用参数col / row?

If you have a 10x10 grid and each tile is 20 pixels then the grid size is 200x200 so you could have x/y values in the range 0-199 如果您有10x10的网格,且每个图块为20像素,则网格大小为200x200,因此您的x / y值范围为0-199

So if you have a coordinate of 25x35 you would simply calculate the row/col values as: 因此,如果坐标为25x35,则只需将行/列值计算为:

int row = 35 / 20;
int column = 25 / 20;

So your method would be something like : 因此,您的方法将类似于:

public Tiles getTile(int x, int y)
{
    int row = y / 20;
    int column = x / 20;

    return Tiles.values()[mapArray[row][column]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM