Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

383
Visualizações
Java 2D array, possible to set individual size?

Quick question about Java 2D arrays; For my tile-based, top-down, 2D game (using swing) I use a 2D array to create a map, like this

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

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)
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. 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).

This is the code I use for checking collision (of course, not working due to 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

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.
My question is, is it possible to define a 2D map array with the size of a tile in-mind? 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 (i.e 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!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This method uses a method within the map class that returns the tile on that specific coordinate, like this

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?

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

So if you have a coordinate of 25x35 you would simply calculate the row/col values as:

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]];
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda