Detecting a particular tile in a tile map HELP!!!

:wink: hi. im currently developing a bomberman type of game . The character is fixed at the center of the screen… the floor does the movement.

THE PROBLEM:
I have a boulder in the game (which is obviously , the player can’t pass though). My question is how can i detect that my player is on a tile that is a boulder so that i can have a condition that the player can’t advance more? i am talking about tiledLayer here… im planning to use sprites instead but my game is already flooded by sprites and i want to retain the tiledLayer.

I have already read a topic here which has the same problem but i didnt got it coz im a newbee here… i just started 2 weeks ago for my OJT. please, if you can give me the simplest and detailed code that a newbee can understand… PLEASE I BEG YOU… HELP me. thanks in advance! :smiley:

PS:
this is the code i used

player.collidesWith(backgroundTiledImage,true)
System.out.println(“you collided to the tiled image”);

but i want to detect only the collision between a particular tile in the tiledLayer(eg. wall,boulder) :wink:

oehm, as far as im am informed even a tiledlayer uses indexes for the tiles. far the simplest solution would be to have a formula for whicht tile is a boulder.
e.g. all tiles after index 30 or whatever …

btw:
i dont understand you code snippet. there should be a sourrounding if-clause, right ?
also, for which reason do you pass the boolean ‘true’ value in the collide-method ?

The problem is that there isn’t a method like:

mySprite.collidesWith(myTiledLayer, boulderIndex, true);

i.e., you can check whether the sprite overlaps a non-transparent part of the tiled layer, but you can’t check whether it overlaps a tile with a particular index.

Sprite and TiledLayer provide no solution to this. However, you must have created the tiled layer from some array of index data; just keep this array around, and check against it using your own code.

For example code, download Forum Nokia’s example ‘MIDP 2.0: Introduction to the Game API v1.0’ (in the Documents->Java section). See the method containsImpassableArea in the class Field: it does exactly what you’re asking for.

HI GUYS! thanks for the ones who read and replied… I finally got how to detect a particular tile in the tiledLayer. you must hard code it.

e.g.

you initialise the tiledLayer; assuming the tiles is 25x25
. it means that tile[0][0] is from 0 to 24 pixels in X and Y axis.
tileX = new int {0,25,50,75,10};
tileY= new int {0,25,50,75,10};
map = new int[][] ={
{0,0,1,1},
{0,0,1,1},
{0,0,1,1},
{0,0,1,1}
};

the method to determine a particular tile is…
for(i =0,i<4,i++){
if((sprite.getX() >= tileX[i])&&(sprite.getX() < tileX[i+1])
tileIndexX = i;
if((sprite.getY() >= tileY[i])&&(sprite.getY() < tileY[i+1])
tileIndexY= i;
}
tiledLayer.getCell(tileIndexX,tileIndexY);

try to move your sprite and make a code to print what kind of tile is underneath your sprite. try it to your application… it may help too. but if you have a better code for detecting a particular tile… pls POET IT HERE! thanks a lot! and i hope this code may help others.

How about:

tileIndexX = sprite.getX() / 25;
tileIndexY = sprite.getY() / 25;

By the way, notice that in your line:
tileX = new int {0,25,50,75,10};
that last number should be 100. I assume that’s just a typo. :slight_smile:

But, notice that your solution assumes that the sprite is just a point, i.e. 1x1 pixels. In reality, your sprite has width and height. This means that it could easily be overlapping several cells at the same time. Do go and check out that Forum Nokia example: it contains example code for exactly this problem. No point reinventing the wheel!

tileIndexX = sprite.getX() / 25;
tileIndexY = sprite.getY() / 25;

y did you dvided it into 25? the program is supposed to get the value of i in the for loop to reference it to the current x&y position of a cell in tiledLayer…

uhh…

anyway… thanks again DAVID for your help. I’m gonna check that article you told me and see for myself how it can help me. again thanks and regards! and yeah… nice observation david on the size of the sprite (1x1) pixel… you are right but i just tweaked my code to make it bigger to the actual size of my sprite… and now… im gonna check the article and compare which is better. THANKS!

Sorry it wasn’t clear; those two lines replace your whole for loop. And you don’t need tileX or tileY either.

Let’s say your sprite is at (x=60,y=30). So tileIndexX = 60/25 = 2 (remember it’s integer division) and tileIndexY = 30/25 = 1. That’s the same result your for loop would have produced, isn’t it?