#movement script wasd
23 messages · Page 1 of 1 (latest)
there are many solutions to this problem. You could also create an array that can contain all the pressed keys. This would scale to as many keys as you want, but I am not sure I find this more readable.
the boolean variables have a simplicity that is easy to write and understand in my opinion.
An object with all key booleans?
KeyIsDown(keycode)
KeyIsDown() as you describe it does not seem to be a part of Processing
p5 python and p5.js have it, though.
You keep bringing up P5.JS / JavaScript solutions, but this is a Processing / Java question.
if (keyPressed && key == 'W') {} etc should work
If you want more than one direction at a time, nest the key checks inside the if (keyPressed) {} statement
Actually, it might work fine doing them separately, but it's more efficient to check once
Or better yet, create a method for handling keys and call it during draw() when keyPressed is true
this is what i ended up with
float clamp(float val, float min, float max) {
if (val<min) return min;
if (val>max) return max;
return val;
}
class Player {
PVector pos;
PVector dir = new PVector(0, 0);
color c;
float r;
float speed;
boolean up, down, left, right, sneak;
Player(PVector _pos, float _r, color _c, float s) {
pos=_pos;
r=_r;
c=_c;
speed=s;
}
void show() {
noStroke();
fill(c);
circle(pos.x, pos.y, r*2);
}
void update() {
dir.x = left ? -1 : 0;
dir.x = right ? 1 : dir.x;
dir.y = down ? 1 : 0;
dir.y = up ? -1 : dir.y;
dir.normalize();
//pos = pos.add(dir);
pos = pos.add(dir.mult(!sneak ? speed : speed/2));
//pos.x = (width+pos.x)%width;
//pos.y = (height+pos.y)%height;
pos.x = clamp(pos.x, 0+r, width-r);
pos.y = clamp(pos.y, 0+r, height-r);
}
void keyPressed() {
//println("pressed");
if (keyCode == SHIFT) sneak=true;
switch (key) {
case 'w' :
case 'W' :
up=true;
break;
case 's' :
case 'S' :
down=true;
break;
case 'a' :
case 'A' :
left=true;
break;
case 'd':
case 'D':
right=true;
break;
}
}
void keyReleased() {
if (keyCode == SHIFT) sneak=false;
switch (key) {
case 'w' :
case 'W' :
up=false;
break;
case 's' :
case 'S' :
down=false;
break;
case 'a' :
case 'A' :
left=false;
break;
case 'd':
case 'D':
right=false;
break;
}
}
}
is this bad coding practices? if so how do you guys suggest doing it better
Processing already has a "clamp" function: constrain()
Another way to handle case-insensitive keys would be to convert the character to uppercase using key = Character.toUpperCase(key); and then you would only need to worry about uppercase letters, reducing the number of cases here. It's not that big of a deal, but it might make things cleaner or take less space.
Looks pretty good tho
thanks constrain will come in handy :D
Sadly, it is not ideal. key always hold the key that was most recently pressed or released. So you can break this if statement pretty easily:
- Press ANY key you want and hold it
- Press and release the
Wkey
-> You are no longer holding W but that statement will still evaluate to true because there is still a key pressed (that first "any" key), and W was the key most recently released.
this code example looks fine. It is extensible and readable. clamp() could be swapped out for constrain() as Loud_Cat pointed out.
Me neither
Oh that's right! How could I forget that you can't store 2 values at once haha
Silly me
Well that would be the issue if they want more than 2 directions at once. Not to mention the problem you brought up too
Using Character.toUpperCase() is still good tho right?
id imagine