java collision and canvas repaint

Discussion in 'Mac Programming' started by markgodley, Mar 19, 2010.

  1. markgodley macrumors regular

    Joined:
    Mar 25, 2009
    #1
    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]
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. markgodley thread starter macrumors regular

    Joined:
    Mar 25, 2009
    #3
    you mean the 0, SPEED are the wrong way round? in:

    spriteKarel.move(0,-SPEED);


    it should be
    spriteKarel.move(-SPEED, 0); ?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.
     
  5. markgodley thread starter macrumors regular

    Joined:
    Mar 25, 2009

Share This Page