Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Detecting a particular tile in a tile map HELP!!!  (Read 1435 times)
0 Members and 1 Guest are viewing this topic.
Offline moonknightskye

Senior Newbie

^_~! love is in the air

« Posted 2004-10-29 01:49:46 »

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.

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! Cheesy

this is the code i used

                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

At the dark MOON, a KNIGHT will appear to clear the SKYE.

Offline Serethos

Junior Devvie

Java games rock!

« Reply #1 - Posted 2004-10-30 08:50:40 »

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

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 ?
Offline davidaprice

Junior Devvie

« Reply #2 - Posted 2004-10-31 07:53:21 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline moonknightskye

Senior Newbie

^_~! love is in the air

« Reply #3 - Posted 2004-11-02 06:50:12 »

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.


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[][] ={

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

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.

At the dark MOON, a KNIGHT will appear to clear the SKYE.

Offline davidaprice

Junior Devvie

« Reply #4 - Posted 2004-11-02 11:40:51 »

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

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!
Offline moonknightskye

Senior Newbie

^_~! love is in the air

« Reply #5 - Posted 2004-11-02 11:58:47 »

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


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!

At the dark MOON, a KNIGHT will appear to clear the SKYE.

Offline davidaprice

Junior Devvie

« Reply #6 - Posted 2004-11-04 11:15:20 »

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?
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (37 views)
2015-08-01 03:58:20

Jesse (24 views)
2015-07-29 04:35:27

Riven (44 views)
2015-07-27 16:38:00

Riven (24 views)
2015-07-27 15:35:20

Riven (27 views)
2015-07-27 12:26:13

Riven (18 views)
2015-07-27 12:23:39

BurntPizza (39 views)
2015-07-25 00:14:37

BurntPizza (48 views)
2015-07-24 22:06:39

BurntPizza (33 views)
2015-07-24 06:06:53

NoxInc (40 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!