GUI不更新简单游戏中的值

时间:2017-04-02 14:00:55

标签: java swing

我正在使用Swing库制作一个简单的2D平台游戏,我试图在GUI中不断显示不同的值。这些值在第一级更新并完美地工作,但是对于下一级别它们保持不变。

GUI代码:

public class MyView extends UserView {

    Player player;

    private Image background;
    private Image newBackground;

    public MyView(World world, Player player, int width, int height) {
        super(world, width, height);
        this.player = player;
        this.background = new ImageIcon("data/rainy.gif").getImage()
                .getScaledInstance(1000, 1000, 1000);
    }

    @Override
    protected void paintForeground(Graphics2D g) {
        g.drawString("Lives left: " + player.getLifeCount(), 650, 20);
        g.drawString("Blue hearts picked: " + player.getBlueHeartCount(), 650, 30);
        g.drawString("Time left: " + player.getTimeCounter(), 650, 50);
     }
    }

到达第二级player.getLifeCount()player.getBlueHeartCount()player.getTimeCounter()时,请冻结并停止更新。

以下是级别1和级别2的相关代码(没有导入),其中值已更改:

public class Level1 extends GameLevel implements ActionListener {

@Override
    public void actionPerformed(ActionEvent e) {
        if (getPlayer().heartPick) {
            timer.stop();
        } else if (player.getTimeCounter() <= 0) {
            System.exit(0);
        } else {
            counter -= 1;
            player.setTimeCounter(counter);
        }
    }

第2级代码:

public class Level2 extends GameLevel implements ActionListener {

@Override
    public void actionPerformed(ActionEvent e) {
//        System.out.println("Action event!");
        if (getPlayer().heartPick) {
            timer.stop();
        } else if (player.getTimeCounter() <= 0) {
            System.exit(0);
        } else {
            counter -= 1;
            player.setTimeCounter(counter);
        }
    }
}

以下是主要类的相关代码:

public class Game {
    private GameLevel world;

    private MyView view;

    private int level;

    // timer
    private Timer timer;

    public Game(){

        // make the world
        level = 1;
        world = new Level1();
        world.populate(this);

        // make a view
        view = new MyView(world, world.getPlayer(), 800, 600);

        // display the view in a frame
        final JFrame frame = new JFrame("Event handling");

        // start!
        world.start();
    }

    // return world
    public GameLevel getWorld() {
            return world;
    }

    // the player in current level
    public Player getPlayer() {
        return world.getPlayer();
    }

    // Progress to next level
    public void goNextLevel() {
        world.stop();

        switch (level) {
            case 4:
                System.exit(0);
            case 2:
                level++;

                // get a new world
                world = new Level2();

                // fill world
                world.populate(this);

                // show the new world in the view
                view.setWorld(world);

                view.setBackground(2);
                world.start();
                break;

            case 3:
                level++;

                //get new world
                world = new Level3();

                //fill world
                world.populate(this);

                // show the new world in the view
                view.setWorld(world);

                view.setBackground(3);
                world.start();
                break;

            default:
                level++;
                break;

        }
    }
}

1 个答案:

答案 0 :(得分:3)

switch中的goNextLevel语句正在根据您要进入的级别而不是您当前所处的级别进行切换。因此,当您在第1级并点击goNextLevel块时,它会转到default。您希望根据以前的级别切换,而不是根据级别进行切换,因为您要在该语句中更新它。