Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

markgodley

macrumors regular
Original poster
Mar 25, 2009
135
0
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?
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]
 
Code:
case UP_PRESSED:
                [COLOR="Green"]spriteKarel.move(0,-SPEED);[/COLOR]
                if (lastDirection != UP) {
                    lastDirection = UP;
                    spriteKarel.setTransform(Sprite.TRANS_MIRROR);
                }
                if (spriteKarel.collidesWith(tlTrees,false)) {
                   [COLOR="green"]this.spriteKarel.move(SPEED, 0)[/COLOR];  //The positive cancels out the above negative
                   [COLOR="green"]// hint: the comment on the above line is wrong.[/COLOR]
                }
                adjustViewport (viewPortX - SPEED, viewPortY);
                break;

You have too much copy-pasting of code. I've hilited the problem areas in green.

I've only hilited one problem area. You repeat the mistake in the case DOWN_PRESSED: code.
 
you mean the 0, SPEED are the wrong way round? in:

spriteKarel.move(0,-SPEED);


it should be
spriteKarel.move(-SPEED, 0); ?
 
you mean the 0, SPEED are the wrong way round? in:

spriteKarel.move(0,-SPEED);

it should be
spriteKarel.move(-SPEED, 0); ?

Think it through. If necessary, write it down as a description for someone who doesn't know exactly what your code is doing. Also, compare the code you said doesn't work to the code you said does work.

I'm not giving a specific answer because debugging skills rest on the ability to see consequences from only a few clues.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.