Movement code messed up

This movement class is rather messed up, how can I fix it? Sometimes it causes lag, and sometimes it simply moves without me pressing the button...

class Player {
  boolean isLeft, isRight, isUp, isDown;


  int x, y, size;
  Player(int _x, int _y, int _size) {
    x = _x;
    y = _y;
    size = _size;
    isUp = false;
    isRight= false;
    isDown = false;
    isLeft = false;


  }

  void display() {
    fill(0);
    ellipse(x, y, size, size);


  }

  void move(){
   if (isUp){
    y--;
   } 
   else if (isDown) {
    y++; 
   }
   if (isRight) {
    x++; 
   }
   else if (isLeft) {
    x--; 
   }


  }


  void akeyTyped() {
   if (keyPressed){
    if (key == 'w') {
      isUp = true;
    } 
    else if (key == 's') {
      isDown = true;
    } 
    else if (key == 'a') {
      isLeft = true;
    } 
    else if (key == 'd') {
      isRight = true;
    }
   }
   else {
  if (key == 'w') {
    isUp = false;
  } 
  else if (key == 's') {
    isDown = false;
  } 
  else if (key == 'a') {
    isLeft = false;
  } 
  else if (key == 'd') {
    isRight = false;
  }
}
  }
}

Answers

  • akeyTyped: first set all to false

  • Now it's even more messed up...

  • use keyReleased to check when the key is not hold anymore

    post your entire code

  • delete line 58 to 71

  • This is what I did so far, exactly as you said.

    class Player {
      boolean isLeft, isRight, isUp, isDown;
    
    
      int x, y, size;
      Player(int _x, int _y, int _size) {
        x = _x;
        y = _y;
        size = _size;
        isUp = false;
        isRight= false;
        isDown = false;
        isLeft = false;
    
    
      }
    
      void display() {
        fill(0);
        ellipse(x, y, size, size);
    
    
      }
    
      void move(){
       if (isUp){
        y--;
       } 
       else if (isDown) {
        y++; 
       }
       if (isRight) {
        x++; 
       }
       else if (isLeft) {
        x--; 
       }
    
    
      }
    
    
      void akeyTyped() {
       if (keyPressed){
        if (key == 'w') {
          isUp = true;
        } 
        else if (key == 's') {
          isDown = true;
        } 
        else if (key == 'a') {
          isLeft = true;
        } 
        else if (key == 'd') {
          isRight = true;
        }
    }
      }
    }
    
  • edited July 27

    Hi @thermonuclear666! I've redone your Player class and included a test sketch for it as well!
    You can play it online here: http://Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp

    /**
     * Player Move (v1.2.1)
     * by GoToLoop (2014/Dec/07)
     *
     * Forum.Processing.org/two/discussion/8511/movement-code-messed-up#Item_6
     * Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
     */
    
    static final int DIAM = 48, SPD = 4, FPS = 60;
    static final color BG = 0350;
    
    Player p;
    
    void setup() {
      size(800, 600);
    
      smooth(3);
      frameRate(FPS);
      ellipseMode(CENTER);
    
      fill(Player.INK);
      stroke(Player.OUTLINE);
      strokeWeight(Player.BOLD);
    
      p = new Player(width >> 1, height >> 1, DIAM, SPD);
    }
    
    void draw() {
      background(BG);
      p.move();
      p.display();
    }
    
    void keyPressed() {
      p.setMove(keyCode, true);
    }
    
    void keyReleased() {
      p.setMove(keyCode, false);
    }
    
    final class Player {
      static final color INK = #008000, OUTLINE = 0;
      static final float BOLD = 2.0;
    
      boolean isLeft, isRight, isUp, isDown;
      int x, y;
      final int d, v;
    
      Player(final int xx, final int yy, final int dd, final int vv) {
        x = xx;
        y = yy;
        d = dd;
        v = vv;
      }
    
      void display() {
        ellipse(x, y, d, d);
      }
    
      void move() {
        final int r = d >> 1;
        x = constrain(x + v*(int(isRight) - int(isLeft)), r, width  - r);
        y = constrain(y + v*(int(isDown)  - int(isUp)),   r, height - r);
      }
    
      boolean setMove(final int k, final boolean b) {
        switch (k) {
        case +'W':
        case UP:
          return isUp = b;
    
        case +'S':
        case DOWN:
          return isDown = b;
    
        case +'A':
        case LEFT:
          return isLeft = b;
    
        case +'D':
        case RIGHT:
          return isRight = b;
    
        default:
          return b;
        }
      }
    }
    
  • Can somebody just fix the mistakes in my code rather than giving me theirs please?

  • post your entire code

  • here

    you didn't set all to false as I told you

    /**
     * Player Move (v1.0)
     * by GoToLoop (2014/Dec/07)
     *
     * forum.processing.org/two/discussion/8511/movement-code-messed-up
     *
     * studio.processingtogether.com/sp/pad/export/ro.91tcpPtI9LrXp/latest
     */
    
    static final int DIAM = 48, SPD = 4, FPS = 60;
    static final color BG = 0350;
    
    Player p;
    
    void setup() {
      size(800, 600, JAVA2D);
    
      smooth(4);
      frameRate(FPS);
      ellipseMode(CENTER);
    
      fill(222);
      stroke(0);
      strokeWeight(2);
    
      p = new Player(width>>1, height>>1, 12);
    }
    
    void draw() {
      background(BG);
      p.move();
      p.display();
      p.akeyTyped();
    }
    
    class Player {
    
      boolean isLeft, isRight, isUp, isDown;
      int x, y, size;
    
      Player(int _x, int _y, int _size) {
        x = _x;
        y = _y;
        size = _size;
        isUp = false;
        isRight= false;
        isDown = false;
        isLeft = false;
      }
    
      void display() {
        fill(0);
        ellipse(x, y, size, size);
      }
    
      void move() {
        if (isUp) {
          y--;
        }
        else if (isDown) {
          y++;
        }
        if (isRight) {
          x++;
        }
        else if (isLeft) {
          x--;
        }
      }
    
    
      void akeyTyped() {
    
        isUp = false;
        isRight= false;
        isDown = false;
        isLeft = false;
    
        if (keyPressed) {
          if (key == 'w') {
            isUp = true;
          }
          else if (key == 's') {
            isDown = true;
          }
          else if (key == 'a') {
            isLeft = true;
          }
          else if (key == 'd') {
            isRight = true;
          }
        }
      }
    }
    //
    
Sign In or Register to comment.