具有转换状态的情况未在公共静态最终int之间切换[关闭]

问题描述 投票:-3回答:1

我正在制作一个游戏,当我在启动暂停菜单时释放问题,当您释放空格键时,游戏将开始。为此,我使用switch-case语句,但是当我单击空格时,大小写不会切换。

    public class GameScreen extends JPanel implements Runnable, KeyListener {
        public static final int GAME_FIRST_STATE = 0;
        public static final int GAME_PLAY_STATE = 1;
        public static final int GAME_OVER_STATE = 2;
        public static final float GRAVITY = 0.1f;
        public static final float GROUND = 130;

        private MainCharacter mainCharacter;
        private Thread thread;
        private Land land;
        private Clounds clouds;
        private EnemiesManager enemiesManager;

        private int gameState = GAME_FIRST_STATE;

        public GameScreen() {
            thread = new Thread(this);
            mainCharacter = new MainCharacter();
            mainCharacter.setX(50);
            land = new Land(this);
            clouds = new Clounds();
            enemiesManager = new EnemiesManager();
        }

        public void startGame() {
            thread.start();
        }

        @Override
        public void run() {
        while(true) {
            try {
                update();
                repaint();
                Thread.sleep(30);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
                }
            }
        }

        public void update() {
            mainCharacter.update();
            land.update();
            clouds.update();
            enemiesManager.update();
        }

        @Override
        public void paint(Graphics g) {
            g.setColor(Color.decode("#f7f7f7"));
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.red);
            g.drawLine(0, (int) GROUND, getWidth(), (int) GROUND);

            switch(gameState) {
            case GAME_FIRST_STATE:
                mainCharacter.draw(g);
                break;
            case GAME_PLAY_STATE:
                clouds.draw(g);
                land.draw(g);
                mainCharacter.draw(g);
                enemiesManager.draw(g);
                break;
            }

        }

        public void keyTyped(KeyEvent e) {

        }

        public void keyPressed(KeyEvent e) {

        }

        public void keyReleaseed(KeyEvent e) {
            switch(e.getKeyCode()) {
                case KeyEvent.VK_SPACE:
                    if(gameState == GAME_FIRST_STATE) {
                        gameState = GAME_PLAY_STATE;
                    }
                    break;
            }
        }
    }

代码不完整。 switch语句必须仅以字符开头,并且会发生这种情况,但是当案例切换时,程序也必须绘制陆地和云层,但是这没有发生,我该如何解决。如果您需要其他课程,请询问。

java switch-statement draw
1个回答
0
投票

我输入错误时,好的代码是:

public class GameScreen extends JPanel implements Runnable, KeyListener {
    public static final int GAME_FIRST_STATE = 0;
    public static final int GAME_PLAY_STATE = 1;
    public static final int GAME_OVER_STATE = 2;
    public static final float GRAVITY = 0.1f;
    public static final float GROUND = 130;

    private MainCharacter mainCharacter;
    private Thread thread;
    private Land land;
    private Clounds clouds;
    private EnemiesManager enemiesManager;

    private int gameState = GAME_FIRST_STATE;

    public GameScreen() {
        thread = new Thread(this);
        mainCharacter = new MainCharacter();
        mainCharacter.setX(50);
        land = new Land(this);
        clouds = new Clounds();
        enemiesManager = new EnemiesManager();
    }

    public void startGame() {
        thread.start();
    }

    @Override
    public void run() {
    while(true) {
        try {
            update();
            repaint();
            Thread.sleep(30);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
            }
        }
    }

    public void update() {
        mainCharacter.update();
        land.update();
        clouds.update();
        enemiesManager.update();
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.decode("#f7f7f7"));
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.red);
        g.drawLine(0, (int) GROUND, getWidth(), (int) GROUND);

        switch(gameState) {
        case GAME_FIRST_STATE:
            mainCharacter.draw(g);
            break;
        case GAME_PLAY_STATE:
            clouds.draw(g);
            land.draw(g);
            mainCharacter.draw(g);
            enemiesManager.draw(g);
            break;
        }

    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_SPACE:
                if(gameState == GAME_FIRST_STATE) {
                    gameState = GAME_PLAY_STATE;
                }
                break;
        }
    }
}

也许会帮助您

© www.soinside.com 2019 - 2024. All rights reserved.