0

i have the following code to make my player move:

class Player {

  PVector direction;
  PVector location;
  float rotation;
  int speed;


  Player() {
    location = new PVector(width/2, height/2);
    speed =2;
  }

  void visualPlayer() {
    direction = new PVector(mouseX, mouseY);
    rotation = atan2(direction.y - location.y, direction.x - location.x)/ PI * 180;
    if (keyPressed) {
      if ((key == 'w' && dist(location.x, location.y, direction.x, direction.y)>5) || (key == 'w' && key == SHIFT && dist(location.x, location.y, direction.x, direction.y)>5)) {
        speed = 2;
        location.x = location.x + cos(rotation/180*PI)*speed;
        location.y = location.y + sin(rotation/180*PI)*speed;

        if (key == SHIFT) {
          speed = 5;
        }
      }
    } else {
      location.x = location.x;
      location.y = location.y;
    }

    println(speed);
    ellipse(location.x, location.y, 10, 10);
  }
}

when i press the w key the player moves in the direction of the mouse. but i want to make the player move faster if i press the shift key. but now my player stops moving when i press the shift key... why is this happening?? any suggestions to help me fix this problem are welcome :)

FutureCake
  • 2,614
  • 3
  • 27
  • 70

1 Answers1

0

These two if statements will never both be true:

if ((key == 'w' ) {
    if (key == SHIFT) {

The key variable will only have one value during a call to the draw() function.

In fact, the key variable will never hold a value of SHIFT. Instead, you need to use the keyCode variable.

And since you're trying to detect multiple key presses, you need to do what I told you to do in your other question: you need to use a set of boolean values that keep track of which keys are pressed, then use them in your draw() function.

Here's a little example that shows exactly what I'm talking about:

boolean wPressed = false;
boolean shiftPressed = false;

void draw() {
  background(0);

  if (wPressed && shiftPressed) {
    background(255);
  }
}

void keyPressed(){
  if(key == 'w' || key == 'W'){
    wPressed = true;
  }
  if(keyCode == SHIFT){
    shiftPressed = true;
  }
}

void keyReleased(){
  if(key == 'w' || key == 'W'){
    wPressed = false;
  }
  if(keyCode == SHIFT){
    shiftPressed = false;
  }
}

More info is in the reference or in this tutorial on user input in Processing.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • @FutureCake I've edited my answer to include a little example showing exactly what I'm talking about. – Kevin Workman Aug 08 '16 at 13:13
  • i did that same as in your example but i kept getting the same error... as i had in my original question :( – FutureCake Aug 08 '16 at 20:26
  • @FutureCake What exact error are you getting? If you're having a new problem, please post an updated [mcve] in a new question. – Kevin Workman Aug 08 '16 at 20:33
  • i don't get any errors. the ellipse just stops moving when i press the shift key. but it doesn't matter anymore i am going for a completely new approach :) – FutureCake Aug 08 '16 at 20:35
  • @FutureCake That sounds like a logic problem. Please post a [mcve] in a new question and we can go from there. – Kevin Workman Aug 08 '16 at 20:41