-1

I have an assignment to build a game bord... I want the user to put the in program the keys he wants to play with. The problem is that Java requires that the values ​​in the switch case will be const... The idea is to create a type inheritance to the KeyListener and then I just add it to his proper player.

the desired Keys are received as a parameter int[] keys

I've seen some people offering a solution for similar problems by adding "static final" unfortunately it does not help

here is my code :

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class PlayerKeyListener implements KeyListener
{

int[] keys;
int playerID;

PlayerKeyListener(int[] keys, int playerID)
{
    this.keys = keys;
    this.playerID = playerID;
}

public void keyPressed(KeyEvent e) 
{ 
    System.out.println("keyPressed code : "+ e.getKeyCode());
    switch (e.getKeyCode()) 
    {
    case keys[KeyboardSettings.UP]:     gameBord.players[playerID].moveUp(); break;
    case keys[KeyboardSettings.DOWN]:   gameBord.players[playerID].moveDown(); break;   
    case keys[KeyboardSettings.LEFT]:   gameBord.players[playerID].moveLeft(); break;
    case keys[KeyboardSettings.RIGHT]:  gameBord.players[playerID].moveRight(); break;
    case keys[KeyboardSettings.BOMB]:   gameBord.players[playerID].addBomb(); break;

    default: gameBord.players[playerID].face = 0;
    }
    e.consume();

}

public void keyReleased(KeyEvent e) 
{
    System.out.println("keyReleased code : "+ e.getKeyCode());
    gameBord.players[playerID].move = false;
    gameBord.players[playerID].pic = 0;
    e.consume();
}

public void keyTyped(KeyEvent e) 
{
    System.out.println("keyTyped code : "+ e.getKeyCode());
    e.consume();
}

}

tnx ;)

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118

1 Answers1

1

Case expressions must be constant expressions.

You answered yourself in the title. You can't have a variable after case keyword.

The way how switch command works is that all the cases (the values after the case keyword) must be known in compile-time.

switch (e.getKeyCode()) {
    case constant1: commands;
    case constant2: commands;
}

keys[KeyboardSettings.UP] is not a constant, it is a variable. Its value is known in run-time. The compiler does not know what value is there and is shouting at you: "Hey, user3142930, I need to know the value of everything which follow the case keyword!"

In your case you simply cannot use switch. You should use a sequence of if-else commands, like this:

// store it in a variable so you do not call the method repeatedly
final int code = e.getKeyCode(); 
// this is instead of your switch
if (code == keys[KeyboardSettings.UP]) {
    ....
} 
else if (code == keys[KeyboardSettings.DOWN]) {
    ...
} 
else if (...) {
   ...
}

The deep reason for it is that you want to have the keys user-configurable, so you simply cannot know their values in compile-time.

You may study Runtime vs Compile time if you have not understood my last sentence.

Community
  • 1
  • 1
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • I know, I was angry with you. I do not like questions like "here is my homework assignment, do it for me". Put in more effort into extracting the relevant code. Do not bother us with your assignment. Ask really a Java-related question. It's very good to place your code here, but not all your code. Only this which is needed. Then people will be more willing to answer and not giving you minuses. – Honza Zidek May 21 '14 at 19:53
  • I have added more explanation for you, but you do not deserve it :) Now you owe me to fix your question so it does not contain any mess. – Honza Zidek May 21 '14 at 20:02
  • You need to use "==" instead of just "=" for comparison. – JustinKSU May 21 '14 at 20:19