Xirexor
Xirexor

Reputation: 19

I don't know why next statement is executed before the previous one?

My game is almost complete except for this error which has limited to proceed further. I am newbie programmer. I have the following code

class End

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 is being executed (which resets score count) before displaying current score count. Although other statements working fine. What I wanted to do is to display score (Not highscore, it works fine), but it displays 1 instead of current score. NOTE: the method resetStates() resets it to 1. But i want to do it after display the score.

The main game class, where game.resetStates can be found is below

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();
    }
}

Lastly, this resetState() occurs when player intersects enemy bounding box, whose class is follow, class 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));   
        }
    }

If my codes are not good, sorry for that, beginner here :P

Upvotes: 1

Views: 85

Answers (1)

lodlock
lodlock

Reputation: 3788

Your render() methods are part of the code's main run() while loop. Once the game.state is changed to STATE.END your End.render() method starts looping. It is being called repeatedly which results in the resetState() being called repeatedly.

You should move any non-graphic logic out of the render() methods and handle them separately.

Upvotes: 1

Related Questions