Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 1123 times)
0 Members and 1 Guest are viewing this topic.
Offline moonknightskye

Senior Newbie




^_~! love is in the air


« Posted 2004-10-29 03: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.

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


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

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

MABUHAY ANG PILIPINAS!
Offline Serethos

Junior Member




Java games rock!


« Reply #1 - Posted 2004-10-30 10: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 ..

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

Junior Member





« Reply #2 - Posted 2004-10-31 08: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!
Play the free demo of Revenge of the Titans!
Offline moonknightskye

Senior Newbie




^_~! love is in the air


« Reply #3 - Posted 2004-11-02 07: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.

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)&&(sprite.getX() <  tileX[i+1])
         tileIndexX = i;
if((sprite.getY() >= tileY)&&(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.

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

MABUHAY ANG PILIPINAS!
Offline davidaprice

Junior Member





« Reply #4 - Posted 2004-11-02 12: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 12: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...

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!

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

MABUHAY ANG PILIPINAS!
Offline davidaprice

Junior Member





« Reply #6 - Posted 2004-11-04 12: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (104 views)
2013-05-17 21:29:12

alaslipknot (113 views)
2013-05-16 21:24:48

gouessej (141 views)
2013-05-16 00:53:38

gouessej (137 views)
2013-05-16 00:17:58

theagentd (148 views)
2013-05-15 15:01:13

theagentd (134 views)
2013-05-15 15:00:54

StreetDoggy (177 views)
2013-05-14 15:56:26

kutucuk (199 views)
2013-05-12 17:10:36

kutucuk (201 views)
2013-05-12 15:36:09

UnluckyDevil (207 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.133 seconds with 21 queries.