This site requires JavaScript, please enable it in your browser!
Greenfoot back
Michelium
Michelium wrote ...

2017/9/28

Making the game stop after a specifc amount of seconds

Michelium Michelium

2017/9/28

#
Hi, I want to let the game stop after a specific amount of seconds for each level. (I will implement the timed level system later on when I got this to work) It has to end after 40 seconds. So I tried to do that this way, since I couldn't figure out another way to do this. But if I run now it's run for about 1 second and then the value of "timeLeft" becomes -1.
private int timeLeft = 40;
    private int counter = 0;
    public void showTimer(){ 
        if (timeLeft <= 0) { 
            JOptionPane.showMessageDialog(null, "You're out of time!");
            Greenfoot.stop();
        }
        counter++;
        if (counter == 35) { 
            timeLeft=-1;
            counter = 0;
        }
        GreenfootImage timer = new GreenfootImage("Time left: " + timeLeft, 26, Color.GREEN, Color.BLACK);
        getWorld().getBackground().drawImage(timer, 40, 92);
    }
danpost danpost

2017/9/28

#
Look at line 10 !!! (closely -- try to understand what exactly you have that line doing)
Michelium Michelium

2017/9/28

#
danpost wrote...
Look at line 10 !!! (closely -- try to understand what exactly you have that line doing)
OMG! Thanks! I just did
timeLeft = timeLeft - 1;
and now it works perfectly!
You need to login to post a reply.