Moving an Object diagonally using two key inputs i.e 'w' + 'd'

edited March 2018 in Questions about Code

creating a football game, need to create a mechanic where the player will move diagonally when two certain keys are pressed in combination. Here's my code so far;

thanks in advance

class Blue {

  PVector pos;
  float radius;
  color Colour = color (0, 0, 255);


  Blue (float x, float y, float r_, int red, int green, int blue) {
    pos = new PVector(x, y);
    radius = r_;
    Colour = color (red, green, blue);
  }

  void displayBlue () {
    fill (Colour);
    strokeWeight (2);
    stroke(255);
    ellipse(pos.x, pos.y, radius, radius);
  }

  void ballCollision () {
  }


  void moveBlue () {

    if ((keyPressed == true) && (key == 'a')) {
      pos.x = pos.x -3;
    }

    if ((keyPressed == true) && (key == 'd')) {
      pos.x = pos.x +3;
    }

    if ((keyPressed == true) && (key == 'w')) {
      pos.y = pos.y -3;
    }

    if ((keyPressed == true) && (key == 's')) {
      pos.y = pos.y +3;
    }


  }
 }
Tagged:

Answers

  • edited March 2018 Answer ✓

    Use a set of boolean variables to track whether particular keys are being pressed. Something like:

    boolean wPressed = false;
    boolean aPressed = false;
    

    Then in the keyPressed() function, set them to true, and in the keyReleased() function, set them to false. Finally, just check the values of the variables in the draw() function.

    Shameless self-promotion: I wrote a tutorial on user input in Processing, including this technique, available here.

  • Hi thanks for the reply, I followed the tutorial on the link you gave, it didn’t work for me, I was wondering if it was to do with the fact that I’m using this within a class? Could you explain how to use keyPressed and KeyReleased within a class?

  • edited March 2018 Answer ✓

    The keyPressed() and keyReleased() functions need to be inside the top-level sketch, not inside a class.

  • Thanks again, i've tried that it's still not moving, i'll post my code below, maybe you'll be able to spot an obvious mistake;

    Blue blue;
    Red red;
    int bScore = 0;
    int rScore = 0;
    boolean wPressed = false;
    boolean dPressed = false;
    boolean sPressed = false;
    boolean aPressed = false;
    
    
    void setup () {
      size (1200, 800);
      frameRate(100);
      startTimer = new Timer (0);
    
    
      ball = new Ball (600, 400, 15); 
      blue  = new Blue (400, 230, 50, 0, 0, 255);
      red = new Red (800, 230, 50, 200, 255, 0, 0);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    void draw () {
      drawPitch ();
    
      ball.display();
      ball.update();
      ball.bCollision();
      ball.pCollision();
    
    
    
      startTimer.countUp();
    
      blue.displayBlue();
      blue.moveBlue();
      blue.boundaryCollision();
    
      blueScore();
    
    
    
      fill(0);
      textSize (20);
      text (startTimer.getTime(), 575, 20);
    
    
      println (mouseX, mouseY);
    }
    
    
    void blueScore() {
    
    
      fill(0);
      textSize (20);
      text(bScore, 10, 20);
    }
    
    
    void keyPressed() {
      if (key == 'a') {
        aPressed = true;
      } else if (key == 'd') {
        dPressed = true;
      } else if (key == 'w') {
        wPressed = true;
      } else if (key == 's') {
        sPressed = true;
      }
    }
    
    void  keyReleased () {
      if (key == 'a') {
        aPressed = false;
      } else if (key == 'd') {
        dPressed = false;
      } else if (key == 'w') {
        wPressed = false;
      } else if (key == 's') {
        sPressed = false;
      }
    }
    
    
    class Blue {
      boolean wPressed = false;
      boolean dPressed = false;
      boolean sPressed = false;
      boolean aPressed = false;
    
    
      PVector pos;
      float radius;
      color Colour = color (0, 0, 255);
    
    
      Blue (float x, float y, float r_, int red, int green, int blue) {
        pos = new PVector(x, y);
        radius = r_;
        Colour = color (red, green, blue);
      }
    
      void displayBlue () {
        fill (Colour);
        strokeWeight (2);
        stroke(255);
        ellipse(pos.x, pos.y, radius, radius);
      }
    
      void boundaryCollision () {
    
        if (pos.x > width-75) { //RIGHT WALL
          pos.x = width-75;
        } else if (pos.x <75) { //LEFT WALL
          pos.x = 75;
        } else if (pos.y >height -45) { //BOTTOM WALL
          pos.y = height-45;
        } else if (pos.y <45) { //TOP WALL
          pos.y = 45;
        }
      }
    
    
      void moveBlue () {
    
    
    
        if (aPressed){
          pos.x = pos.x -3;
        }
    
        if (dPressed){
          pos.x = pos.x +3;
        }
    
        if (wPressed){
          pos.y = pos.y -3;
        }
    
        if (sPressed){
          pos.y = pos.y +3;
        }
    
      }
     }
    
  • Sorryyyyyyy i figured it out :P, i needed to rename the booleans in both the keyPressed & keyReleased functions to 'blue.'

    thanks so much for the help

  • Note that you only need one set of these variables, since they're global for your whole sketch. You don't need another copy of them inside your class.

Sign In or Register to comment.