Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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 933 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);
sprit.setFrame(Cool;
} else {
sprit.setFrame(9);
}
}
/*the same on RIGHT_PRESSED, UP and DOWN*/


/* if no key is pressed */
if ((keyStates == 0)) {
sprit.setFrame(9);

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

}

/* the method that handles the scolling */

if (terrainScroll < 0 && scrolling == true ) {
terrainScroll += 2;
tBackground.setPosition(0,terrainScroll);
}

}//end of method checkkeys...

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

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

Perhaps someone has any code samples?

Thanks in advance!

/arnold
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?

/Arnold
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.:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
//Move the background
terrainScroll += 2;
tBackground.setPosition(0,terrainScroll);

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

//render background
background.render();

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

<Colonel-Pepper>
Good luck, StarFox!
</Colonel-Pepper>

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.

 

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

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!