Multiple Keys Pressed -> one released, action stops
in
Programming Questions
•
2 years ago
Hello!
I'm working on some little jump n run game for an assignment.
For that, of course, I need multiple keys pressed to work. (Until now) we only have the basic actions move left, move right and jump, which all by themself are working as they should.
For multiple keys pressed I tried to apply option 2 from http://wiki.processing.org/w/Multiple_key_presses which actually indeed works as long as the keys are pressed.
But once one of the keys is released, the other action will stop (e.g. move left and jump - moving left while pressing jump (pressing both at the same time) is possible, moving left stops when releasing jump, even though left is still pressed). This is a problem I'd like to fix.
The code extract this applies to is here, if the whole code is needed, I can provide it, but I'd like to avoid that as it is already a rather complex program.
boolean[] keys = new boolean[526];
//[...]
void move() {
if (platformTouched && (charY <= platform[onPlatform].platformY) && charTop) {
charY = platform[onPlatform].platformY;
}
if (!(onGround||(platformTouched && charTop))) {
charY = charY + speed;
speed = speed - gravity;
if (speed <= 0) {
charTop = true;
}
}
else if (onGround) { //||charY == platform[0].platformY
charTop = false;
charY = 0;
}
if (keyPressed) {
if (checkKey(RIGHT)) {
//println("Char moves right");
charX+=0.3;
}
if(checkKey(LEFT)) {
//println("Char moves left");
charX-=0.3;
}
if(checkKey(CONTROL)&& (onGround||(platformTouched && charTop))) {
//println("Char jumps");
charTop = false;
jump();
/*Der Charakter soll nach oben springen, aber ab einer
bestimmten Höhe wieder fallen.
Danach soll es möglich sein, ihn in eine bestimmte
Richtung springen zu lassen*/
}
}
}
boolean checkKey(int k)
{
if (keys.length >= k) {
return keys[k];
}
return false;
}
void keyPressed()
{
keys[keyCode] = true;
//println(KeyEvent.getKeyText(keyCode));
}
void keyReleased()
{
keys[keyCode] = false;
}
}
I'm working on some little jump n run game for an assignment.
For that, of course, I need multiple keys pressed to work. (Until now) we only have the basic actions move left, move right and jump, which all by themself are working as they should.
For multiple keys pressed I tried to apply option 2 from http://wiki.processing.org/w/Multiple_key_presses which actually indeed works as long as the keys are pressed.
But once one of the keys is released, the other action will stop (e.g. move left and jump - moving left while pressing jump (pressing both at the same time) is possible, moving left stops when releasing jump, even though left is still pressed). This is a problem I'd like to fix.
The code extract this applies to is here, if the whole code is needed, I can provide it, but I'd like to avoid that as it is already a rather complex program.
boolean[] keys = new boolean[526];
//[...]
void move() {
if (platformTouched && (charY <= platform[onPlatform].platformY) && charTop) {
charY = platform[onPlatform].platformY;
}
if (!(onGround||(platformTouched && charTop))) {
charY = charY + speed;
speed = speed - gravity;
if (speed <= 0) {
charTop = true;
}
}
else if (onGround) { //||charY == platform[0].platformY
charTop = false;
charY = 0;
}
if (keyPressed) {
if (checkKey(RIGHT)) {
//println("Char moves right");
charX+=0.3;
}
if(checkKey(LEFT)) {
//println("Char moves left");
charX-=0.3;
}
if(checkKey(CONTROL)&& (onGround||(platformTouched && charTop))) {
//println("Char jumps");
charTop = false;
jump();
/*Der Charakter soll nach oben springen, aber ab einer
bestimmten Höhe wieder fallen.
Danach soll es möglich sein, ihn in eine bestimmte
Richtung springen zu lassen*/
}
}
}
boolean checkKey(int k)
{
if (keys.length >= k) {
return keys[k];
}
return false;
}
void keyPressed()
{
keys[keyCode] = true;
//println(KeyEvent.getKeyText(keyCode));
}
void keyReleased()
{
keys[keyCode] = false;
}
}
1