Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
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  
  tile collission in MIDP 2.0 and scrolling?  (Read 1509 times)
0 Members and 1 Guest are viewing this topic.
Offline arnold

Junior Newbie

Java games rock!

« Posted 2004-11-30 07:28:57 »

Hi all!

I have the following problem - I want my airplane to be able to collide with a tile.

this are the methods I'm now using but they doesn't seem to work as I want them to...

In the GameCanvas class:

/*method to check for collission*/

boolean containsImpassableArea(int x, int y, int widths, int heights) {
int rowMin = y / tHeight;
int rowMax = (y + heights - 1) / tHeight;
int columnMin = x / tWidth;
int columnMax = (x + widths - 1) / tWidth;

for (int row = rowMin; row <= rowMax; ++row) {
for (int column = columnMin; column <= columnMax; ++column) {
int cell = tBakgrund.getCell(column, row);
if (cell == STOP_TILE) {
return true;
return false;

/*method to check for keystates and invokes containsImpassableArea */

private void checkKeys() throws Exception {

int keyStates = getKeyStates();

if ((keyStates & LEFT_PRESSED) != 0) {
if (sprit.getX() > 0
&& !containsImpassableArea(sprit.getX() - 4, sprit.getY(),
sprit.getWidth(), sprit.getHeight())) {
sprit.move(-4, 0);
} else {
/*the same on RIGHT_PRESSED, UP and DOWN*/

/* if no key is pressed */
if ((keyStates == 0)) {

if(containsImpassableArea(sprit.getX(), sprit.getY() - 4,
sprit.getWidth(), sprit.getHeight())) { scrolling = false; }
scrolling = true;


/* the method that handles the scolling */

if (terrainScroll < 0 && scrolling == true ) {
terrainScroll += 2;

}//end of method checkkeys...

my tiles are defined in a two-dimentional array/matrice

ok.... why doesn't it work it some kind of layerproblem (sprite and tiles not in the same layers)?

Perhaps someone has any code samples?

Thanks in advance!

Offline arnold

Junior Newbie

Java games rock!

« Reply #1 - Posted 2004-12-02 09:56:14 »

Is there nobody who has written a vertical scrolling game and knows how to make the spaceship collide with a tile?

Please help..... anyone who has the code or knows where to get it / other forums?

Offline jbanes

JGO Coder

Projects: 1

"Java Games? Incredible! Mr. Incredible, that is!"

« Reply #2 - Posted 2004-12-02 10:49:17 »

This isn't an issue specific to MIDP, so any collision code you find will do. (hint, hint) The code you posted looks more or less ok, with a few possible caveats:

1. Are you sure you want to be scrolling only when the user presses a key?

2.  ++row != row++. It has been a while since I've figured out how that affects loops, but you may want to verify the start and end points you're getting.

Beyond that, I'd have to see your rendering code. I have a sneaky suspicion that you're mixing coordinate systems and getting into trouble. Keep in mind that if your sprites are in the screen space, then they won't properly align to the scrolling background. The "correct" way to handle this situation is to use the background coordinates for the space ship, then render the space ship minus the scrolling offset. i.e.:

//Move the background
terrainScroll += 2;

//Move the sprite relative to the background
sprite.move(0, -2);

//render background

//render sprite at sprite.x, sprite.y - terrainScroll
//the values passed are offsets
sprite.render(0, -terrainScroll);

A few other notes:

1. Please use the [ code ] tag when posting code. That makes it more readable and more likely you'll get a response.

2. Feel free to spell out your variable names. You can always use an obfuscator to shrink your class file for you. As for typing, a good IDE should help with auto-complete.

Good luck, StarFox!

Java Game Console Project
Last Journal Entry: 12/17/04
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Ecumene (53 views)
2017-09-30 02:57:34

theagentd (76 views)
2017-09-26 18:23:31

cybrmynd (184 views)
2017-08-02 12:28:51

cybrmynd (182 views)
2017-08-02 12:19:43

cybrmynd (189 views)
2017-08-02 12:18:09

Sralse (198 views)
2017-07-25 17:13:48

Archive (747 views)
2017-04-27 17:45:51

buddyBro (881 views)
2017-04-05 03:38:00

CopyableCougar4 (1429 views)
2017-03-24 15:39:42

theagentd (1319 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 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!