Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

388
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda