Home > Mobile >  How to change static Variables in Java
How to change static Variables in Java

Time:04-22

How do I change the data exchange to not use statics. My Prof told me I should avoid it, but I dont know how to improve here. For example I brought the class Controls. Any Ideas? I really don't know what to do and need help. Thank You!!

package game2048;

public class GameBody {
    public static final ColorDescription COLORS = new ColorDescription();
    public static final Window WINDOW = new Window("2048");
    public static final Controls CONTROLS = new Controls();
    public static final Board BOARD = new Board(4);

}
public class Controls implements KeyListener {

    @Override
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {

        int keyCode = e.getKeyCode();

        switch (keyCode) {
            case KeyEvent.VK_UP:
                GameBody.BOARD.moveUp();
                break;
            case KeyEvent.VK_DOWN:
                GameBody.BOARD.moveDown();
                break;
            case KeyEvent.VK_LEFT:
                GameBody.BOARD.moveLeft();
                break;
            case KeyEvent.VK_RIGHT:
                GameBody.BOARD.moveRight();
                break;
            case KeyEvent.VK_ESCAPE:
                GameBody.WINDOW.dispose();
                break;
            default:
                break;
        }

        GameBody.BOARD.isGameOver();
        GameBody.WINDOW.repaint();

    }

    public void bind() {
        GameBody.WINDOW.addKeyListener(this);
    }

    public void unbind() {
        GameBody.WINDOW.removeKeyListener(this);
    }

}

CodePudding user response:

At first, remove all static keywords.

Then, in you main(), create an instance of your Gameboy using:

GameBody gamebody = new Gamebody();

And be aware of keyPressed. It might be a better idea to use keyReleased.

CodePudding user response:

Something like this:

package game2048;

public class GameBody {
    public final ColorDescription COLORS = new ColorDescription();
    public final Window WINDOW = new Window("2048");
    public final Controls CONTROLS = new Controls();
    public final Board BOARD = new Board(4);

}
public class Controls implements KeyListener {
    private GameBody gamebody = GameBody();

    @Override
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {

        int keyCode = e.getKeyCode();

        switch (keyCode) {
            case KeyEvent.VK_UP:
                gamebody.BOARD.moveUp();
                break;
            case KeyEvent.VK_DOWN:
                gamebody.BOARD.moveDown();
                break;
            case KeyEvent.VK_LEFT:
                gamebody.BOARD.moveLeft();
                break;
            case KeyEvent.VK_RIGHT:
                gamebody.BOARD.moveRight();
                break;
            case KeyEvent.VK_ESCAPE:
                gamebody.WINDOW.dispose();
                break;
            default:
                break;
        }

        gamebody.BOARD.isGameOver();
        gamebody.WINDOW.repaint();

    }

    public void bind() {
        gamebody.WINDOW.addKeyListener(this);
    }

    public void unbind() {
        gamebody.WINDOW.removeKeyListener(this);
    }

}
  •  Tags:  
  • java
  • Related