I am trying to make a 2 player game so as a result I am passing each players controls through the player class's constructor. This works well for normal keys but 1 player uses the arrow keys. How do you do you use the arrow keys as inputs as it is throwing the error "Badly formed character constant (expecting quote, got E)".
My key listener:
void keyPressed(){
for (Player p:g.players){
p.checkforInputs(Character.toLowerCase(key));
}
}
My input checker:
void checkforInputs(char k){
if(k==moveLeftKey){...
Creating the player class and passing it the controls:(all arguments are char)
players.add(new Player('LEFT','RIGHT','UP','DOWN','<','>','m'));
CodePudding user response:
Does your code compile correctly with your example ? 'LEFT' is not a character.
Additionally, an arrow key from the keyboard is not a character! It's a sequence of three characters that need to be decoded. So for example the left arrow key can be the string "\033[D" ('\033' is the escape character).
Can you share more detailled code where we can see how "key" is obtained and when keyPressed is called ?
CodePudding user response:
Example to detect special keys:
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
//Do Something
}
}
}
Please see processing keyboard input docs for more details.