java - make speed 5 when shift key is pressed -


i have following code make 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 press w key player moves in direction of mouse. want make player move faster if press shift key. player stops moving when press shift key... why happening?? suggestions me fix problem welcome :)

these 2 if statements never both true:

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

the key variable have 1 value during call draw() function.

in fact, key variable never hold value of shift. instead, need use keycode variable.

and since you're trying detect multiple key presses, need told in your other question: need use set of boolean values keep track of keys pressed, use them in draw() function.

here's little example shows 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 in the reference or in this tutorial on user input in processing.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -