Using [W][A][S][D]+[SHIFT] keys
in
Programming Questions
•
5 months ago
Hello! I've been searching everywhere for this (including this forum) and haven't been able to figure it out. I'm attempting to use [W][A][S][D] and [SHIFT] to steer a square around the screen, but I can't seem to figure out the shift part. It should essentially steer in the standard holonomic FPS style, but I'm having problems. The [SHIFT] key
should toggle the variable which corresponds to the "speed". I don't currently have access to the modified version, but I can assure you it didn't work. This is what I've been trying to modify.
I apologize if I forgot anything. Also, if it wasn't apparent, we're both using Eclipse for compiling, which isn't our problem. Can anyone see what we're doing wrong? Thanks!
- import processing.core.*;
public class Key_WASD extends PApplet {
public static void main(String args[]) {
PApplet.main(new String[] { "--present", "Key_WASD" });
}
final static int NORTH = 1;
final static int EAST = 2;
final static int SOUTH = 4;
final static int WEST = 8;
int result;
float x,y,a;
public void setup() {
size(displayWidth,displayHeight);
frameRate(300);
noCursor();
result = 0;
x = width/2;
y = height/2;
a = 3;
}
public void draw() {
background(0);
switch(result) {
case NORTH: y-=a; break;
case EAST: x+=a; break;
case SOUTH: y+=a; break;
case WEST: x-=a; break;
case NORTH|EAST: y-=a; x+=a; break;
case NORTH|WEST: y-=a; x-=a; break;
case SOUTH|EAST: y+=a; x+=a; break;
case SOUTH|WEST: y+=a; x-=a; break;
case NORTH|SOUTH|EAST: x+=a; break;
case NORTH|SOUTH|WEST: x-=a; break;
case EAST|WEST|NORTH: y-=a; break;
case EAST|WEST|SOUTH: y+=a; break;
}
if (x>displayWidth){x=0;}
if (x<0){x=displayWidth;}
if (y>displayHeight){y=0;}
if (y<0){y=displayHeight;}
fill(255);
rect(x,y,10,10);
}
public void keyPressed(){
switch(key) {
case('w'):case('W'):result |=NORTH;break;
case('d'):case('D'):result |=EAST;break;
case('s'):case('S'):result |=SOUTH;break;
case('a'):case('A'):result |=WEST;break;
}
}
public void keyReleased(){
switch(key) {
case('w'):case('W'):result ^=NORTH;break;
case('d'):case('D'):result ^=EAST;break;
case('s'):case('S'):result ^=SOUTH;break;
case('a'):case('A'):result ^=WEST;break;
}
}
}
- import processing.core.PApplet;
import processing.core.PVector;
public class Key_WASDShift extends PApplet {
public static void main(String args[]) {
PApplet.main(new String[] { "--present", "Key_WASDShift" });
}
boolean left;
boolean right;
boolean down;
boolean up;
boolean SOOPASPEED;
PVector pos = new PVector(500, 300);
public void setup() {
size(displayWidth, displayHeight);
frameRate(60);
}
public void draw() {
if (left) {
if (SOOPASPEED) {
pos.x -= 5;
}
else {
pos.x -= 3;
}
}
if (right) {
if (SOOPASPEED) {
pos.x += 5;
}
else {
pos.x += 3;
}
}
if (down) {
if (SOOPASPEED) {
pos.y += 5;
}
else {
pos.y += 3;
}
}
if (up) {
if (SOOPASPEED) {
pos.y -= 5;
}
else {
pos.y -= 3;
}
}
stroke(255);
fill(255);
background(0);
rectMode(CENTER);
rect(pos.x, pos.y, 10, 10);
}
public void keyPressed() {
if (key == 'a') {
left = true;
SOOPASPEED = false;
}
else if (key == 'A') {
left = true;
SOOPASPEED = true;
}
if (key == 'd') {
right = true;
SOOPASPEED = false;
}
else if (key == 'D') {
right = true;
SOOPASPEED = true;
}
if (key == 's') {
down = true;
SOOPASPEED = false;
}
else if (key == 'S') {
down = true;
SOOPASPEED = true;
}
if (key == 'w') {
up = true;
SOOPASPEED = false;
}
else if (key == 'W') {
up = true;
SOOPASPEED = true;
}
}
public void keyReleased() {
if (key == 'a' || key == 'A') {
left = false;
}
if (key == 'd' || key == 'D') {
right = false;
}
if (key == 's' || key == 'S') {
down = false;
}
if (key == 'w' || key == 'W') {
up = false;
}
}
}
I apologize if I forgot anything. Also, if it wasn't apparent, we're both using Eclipse for compiling, which isn't our problem. Can anyone see what we're doing wrong? Thanks!
1