我不知道为什么下一个陈述在前一个陈述之前被执行了?

时间:2015-12-10 17:06:42

标签: java

我的游戏几乎完成,但此错误仅限于继续进行。我是新手程序员。我有以下代码

课程结束

public class End 
{
    public void render(Graphics g)
    {
        Font font = new Font("TimesNewRoman", Font.BOLD, 15);
        Font font2 = new Font("TimesNewRoman", Font.BOLD, 20);

        g.setFont(font);
        g.setColor(Color.CYAN);

        g.setFont(font2);
        g.setColor(Color.WHITE);

        tex.setDead();
        g.drawString("Died !", 590, 240);
        g.drawImage(tex.player, 590, 250, game);

        g.drawString("Survival Score: " + game.count, 530, 360);

        if(game.count > FileManager.getHighScore())
        {
            g.setColor(Color.YELLOW);
            g.drawString("Your Survival will not be forgotten !", 480, 400);
        }

        g.drawString("Press 'Enter' for retry...", 500, 600);
        g.drawString("Press 'Space' to go Main Menu...", 460, 640);

        FileManager.setHighScore(game.count);

        game.resetStates();
    }
}

正在执行game.resetStates(重置得分计数),然后显示当前得分计数。虽然其他声明工作正常。我想要做的是显示分数(不高分,它工作正常),但它显示1而不是当前分数。注意:方法resetStates()将其重置为1.但我想在显示分数后执行此操作。

可以找到game.resetStates的主要游戏类位于

之下
public class Game extends Canvas implements Runnable
{
    public static int count = 1;
    private End end;

    public static enum STATE{
        MENU,
        SCORE,
        START,
        GAME,
        PAUSE,
        END
    };

    public static STATE State = STATE.MENU;

    public void init()
    {
        requestFocus();
        end = new End(this, tex);
        c.createEnemy(count);
        score = new Font("TimesNewRoman", Font.BOLD, 15);

    }

    private synchronized void start()
    {
        if(running)
            return;

        running = true;
        thread = new Thread(this);
        thread.start();
    }

    private synchronized void stop()
    {
        if(!running)
            return;

        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.exit(1);
    }

    public void run()
    {
     //That Frame Rate stuff.
    }

    private void tick()
    {
        if(State == STATE.GAME)
        {
            player.tick();
            c.tick();
        }
    }

    private void render()
    {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null)
        {
            createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        //================================

        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);


        if(State == STATE.GAME)
        {
            g.drawImage(map, 0, 0, getWidth(), getHeight(), this);
            player.render(g);

            c.render(g);

            g.drawImage(mapVision, 0, 0, getWidth(),  getHeight(), this);

            g.setFont(score);
            g.setColor(Color.WHITE);
            g.drawString("Survival: " + count, 620, 30);
            g.drawString("Press 'Enter' to Pause...", 10, 710);
        }
        else if(State == STATE.MENU)
        {
            menu.render(g);
        }
        else if(State == STATE.SCORE)
        {
            hol.render(g);
        }
        else if(State == STATE.START)
        {
            start.render(g);
        }
        else if(State == STATE.PAUSE)
        {
            pause.render(g);
        }
        else if(State == STATE.END)
        {
            end.render(g);
        }

        //================================
        g.dispose();
        bs.show();
    }

    public void resetStates()
    {
        count = 1;
        player.setX(640);
        player.setY(360);
        c.removeAllEntities();
        c.createEnemy(1);
        tex.setDown();
    }
}

最后,当玩家与敌人的边界框相交时,会发生这种resetState(),其类是跟随的,类Enemy

        public void tick()
        {
            y+= speed;
            if(y >= (Game.HEIGHT * Game.SCALE))
            {
                speed = r.nextInt(3) + 2;
                y = 0;
                x = r.nextInt(Game.WIDTH * Game.SCALE);
                game.count ++;
                game.enemyArmy();
            }   
            if(this.getBody().intersects(game.player.getBody()))
            { 
                game.State = STATE.END;
            }

        }

        public void render(Graphics g)
        {
            g.drawImage(tex.spike, x, y, null);
        }

        public int getX() {

            return x;
        }

        public int getY() {

            return y;
        }

        public Rectangle getBody() 
        { 
            return new Rectangle(this.x, this.y + 10, this.tex.spike.getWidth(null), this.tex.spike.getHeight(null));   
        }
    }

如果我的代码不好,抱歉,初学者:P

1 个答案:

答案 0 :(得分:1)

您的render()方法是代码的主要run()while循环的一部分。一旦game.state更改为STATE.END您的End.render()方法开始循环。它被重复调用,导致重复调用resetState()。

您应该将任何非图形逻辑移出render()方法并单独处理它们。