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(8);
} 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