Collision on tilemaps

i use the following to create the map

int[][] A = {
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{12, 12, 12, 12, 7, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{1, 1, 1, 1, 11, 5, 5, 5, 5, 5, 4, 5, 5, 5},
{3, 1, 1, 1, 11, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{3, 1, 1, 1, 11, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{1, 1, 1, 1, 11, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{1, 1, 1, 1, 11, 5, 5, 5, 5, 6, 12, 12, 12, 12},
{10, 10, 10, 10, 9, 5, 5, 5, 5, 13, 2, 2, 2, 2},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 13, 2, 2, 3, 2},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 13, 2, 2, 2, 2},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 13, 2, 3, 2, 2},
{5, 4, 5, 5, 5, 5, 5, 5, 5, 13, 2, 2, 2, 2},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 8, 10, 10, 10, 10}

     };

    for(across = 0; across < width ; across++) {
        for(vert = 0; vert < height ; vert++) {
            if (A[vert][across] == 1) 
            {

                g2.drawImage(gras,across*32,vert*32,this);
            } 
            
            else if(A[vert][across]==2)
            {
                g2.drawImage(tree,across*32,vert*32,this);

             }
            
            else if(A[vert][across]==3)
            {

                g2.drawImage(mount,across*32,vert*32,this);

        	}
            
            else if(A[vert][across]==4)
            {

                g2.drawImage(seamount,across*32,vert*32,this);
                
        	}
            
            else if(A[vert][across]==5)
            {

        		g2.drawImage(sea,across*32,vert*32,this);    
        		
        	} 
            
            else if(A[vert][across]==6)
            {

        		g2.drawImage(sbr,across*32,vert*32,this);    
        		
        	} 
            
            else if(A[vert][across]==7)
            {

        		g2.drawImage(sbl,across*32,vert*32,this);    
        		
        	} 
            
            else if(A[vert][across]==8)
            {
        		
        		g2.drawImage(str,across*32,vert*32,this);    
        		
        	} 
            
            else if(A[vert][across]==9)
            {
        		
        		g2.drawImage(stl,across*32,vert*32,this);    
        		
        	} 
            
            else if(A[vert][across]==10)
            {
        		
        		g2.drawImage(shoretop,across*32,vert*32,this);    
        		
        	} 
            
            else if(A[vert][across]==11)
            {
        		
        		g2.drawImage(shoreleft,across*32,vert*32,this);    
        		
        	} 
            
            else if(A[vert][across]==12)
            {
        		
        		g2.drawImage(shorebottom,across*32,vert*32,this);    
        		
        	} 

            else
        	{
        		
        		g2.drawImage(shoreright,across*32,vert*32,this);    
        		
        	}
        }
    }

is there any way to set collision like J2ME?

	      // Returns to the previous position if there is an obstacle	   
	    if (map.getCell(shipX, shipY) == 4 || map.getCell(shipX, shipY) == 6 || 
	    		  map.getCell(shipX, shipY) == 7 || map.getCell(shipX, shipY) == 8
	    		  || map.getCell(shipX, shipY) == 9 || map.getCell(shipX, shipY) == 10 ||
	    		  map.getCell(shipX, shipY) == 11 || map.getCell(shipX, shipY) == 12
	    		 || map.getCell(shipX, shipY) == 13 || map.getCell(shipX, shipY) == 14) 
	    { 	
	    	 shipX = preX; 	
	    	 shipY = preY;

like when it hits a certain location or a tile it will be stopped

from what i know j2me count by tile , mine is count by corrdinates

anyway to make a tile blocked?

thanks!

Just create some kind of Tile class that contains one image and a boolean flag indicating whether the tile is passable. Instead of the boolean flag, you could have some kind of enum for what types of units can step on the tile (if there’s different units that can step on different tiles)

And, please, make your life easier by putting the Tiles in an array instead of having those if statements.

i don’t really understand i thought i have put the map in the array

in the if else i was trying to paint the image into the tiles is there another way?

i do not understand what you mean by not using the if else

is there another way?

Sure. Put indexes into the 2d map array, which you use to determine which tile (from some 1d array) to draw.

g2.drawImage(tiles[A[vert][across]],across32,vert32,this);

edit: You don’t need that ImageObserver stuff. You can just use null there.

In fact, you don’t even need to store the indices at all. Instead, you can store references to the actual Tile objects themselves. That isn’t always the best idea, but you could do it.

Generally, I have some kind of map data structure (usually loaded from a file). It stores indices into the Tile array (and possibly some kind of description of the tiles themselves in the file’s header). This is used to put the map positions to the actual Tile objects. Whether you do it this way or not is up to you.