Hey,
Im having trouble with the left and right collisions and screen repaint with my Java ME game.
the key_left_pressed and key_right_pressed are functioning correctly. But the up and down are not. I assume its the move part but ive tried many combinations but no look.
When the sprite hits an object going left or right the character stops as it should. But when it hits an object going up or down it goes through the object in a diagonal line.
Any ideas?
Im having trouble with the left and right collisions and screen repaint with my Java ME game.
the key_left_pressed and key_right_pressed are functioning correctly. But the up and down are not. I assume its the move part but ive tried many combinations but no look.
When the sprite hits an object going left or right the character stops as it should. But when it hits an object going up or down it goes through the object in a diagonal line.
Any ideas?
Code:
private void checkUserInput()
{
// get the state of keys
int keyState = getKeyStates();
switch (keyState) {
case LEFT_PRESSED:
spriteKarel.move(-SPEED,0);
if (lastDirection != LEFT) {
lastDirection = LEFT;
spriteKarel.setTransform(Sprite.TRANS_ROT270);
}
if (spriteKarel.collidesWith(tlTrees,false)) {
this.spriteKarel.move(SPEED, 0); //The positive cancels out the above negative
}
//Check if sprite reaches the edge of the Base Layer
if (spriteKarel.getX() < 0 || spriteKarel.getY() < 0
|| spriteKarel.getX() > tlBase.getWidth() - spriteKarel.getWidth())
{
spriteKarel.move(SPEED, 0);
}
adjustViewport (viewPortX - SPEED, viewPortY);
break;
case RIGHT_PRESSED:
spriteKarel.move(SPEED,0);
if (lastDirection != RIGHT) {
lastDirection = RIGHT;
spriteKarel.setTransform(Sprite.TRANS_ROT90);
}
if (spriteKarel.collidesWith(tlTrees,false)) {
this.spriteKarel.move(-SPEED, 0); //The positive cancels out the above negative
}
adjustViewport (viewPortX + SPEED, viewPortY);
break;
[COLOR="Red"] case UP_PRESSED:
spriteKarel.move(0,-SPEED);
if (lastDirection != UP) {
lastDirection = UP;
spriteKarel.setTransform(Sprite.TRANS_MIRROR);
}
if (spriteKarel.collidesWith(tlTrees,false)) {
this.spriteKarel.move(SPEED, 0); //The positive cancels out the above negative
}
adjustViewport (viewPortX - SPEED, viewPortY);
break;
case DOWN_PRESSED:
spriteKarel.move(0,SPEED);
if (lastDirection != DOWN) {
lastDirection = DOWN;
spriteKarel.setTransform(Sprite. TRANS_MIRROR_ROT180);
}
if (spriteKarel.collidesWith(tlTrees,false)) {
this.spriteKarel.move(SPEED, 0); //The positive cancels out the above negative
}
adjustViewport (viewPortY + SPEED, viewPortX);[/COLOR]