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

2019/4/9

Timer keeps adding 0.02

renniemm renniemm

2019/4/9

#
This is the "timer" part of my act method in my world subclass:
showTime();
        if(timer!=0.00){
            timer --;
            if(timer == 0.00){ //player runs out of time
                Greenfoot.stop();
                failed(); // displays text saying "try again"
                Greenfoot.playSound("fail.mp3");

            }
        }
and this is showTime:
private void showTime(){
        showText("Time: "+(seconds.format(timer/60.0)), 60, 20);
    }
and I have this before the act method:
private double timer = 1500.0;
    DecimalFormat seconds = new DecimalFormat("#.00");
The problem is that when the timer ends (going for 25 seconds) and the game stops, instead of reading Time: 0.00 like it's supposed to, it reads Time: 0.02 and I haven't a clue why. This tiny little thing is bothering me so much. If you have any ideas, I would appreciate it!
Super_Hippo Super_Hippo

2019/4/9

#
You call the showTime method which calls the showText method at the beginning of the act method. Afterwards, you decrease the timer and stop the execution of the scenario when it is 0. So the scenario stops and the showTime method isn't called again to show the correct time. It basically never shows the correct time. It is always one act cycle behind. To fix it, move the call of the showTime method from line 1 to after line 3 (or to line 11 or something).
renniemm renniemm

2019/4/9

#
Super_Hippo wrote...
You call the showTime method which calls the showText method at the beginning of the act method. Afterwards, you decrease the timer and stop the execution of the scenario when it is 0. So the scenario stops and the showTime method isn't called again to show the correct time. It basically never shows the correct time. It is always one act cycle behind. To fix it, move the call of the showTime method from line 1 to after line 3 (or to line 11 or something).
I get it now! That was an easy fix. Thanks! I actually have one more problem I forgot to add, if you don't mind. When my game starts, it randomly spawns "monsters" around the world (the player can't touch or the game ends) as well as stars that the player (a cat character) has to collect. At first, some of the monsters and stars would be so close together that it was unfair, so I added this to the monster class (and a similar one to the star class).
protected void addedToWorld(World w)
{
    while (!getObjectsInRange(10, Monster.class).isEmpty() || !getObjectsInRange(50, Star.class).isEmpty()|| !getObjectsInRange(10, Cat.class).isEmpty())
    {
        setLocation(10+Greenfoot.getRandomNumber(getWorld().getWidth()), 10+Greenfoot.getRandomNumber(getWorld().getHeight()));
    }
}
}
While this fixed that issue, once in a while I still get monsters that spawn right under the cat when the game starts (the cat always starts in the center) and the game is immediately over. I thought having the !getObjectsInRange(10, Cat.class).isEmpty() would fix that but it did not. Now I'm not sure what to do.
Super_Hippo Super_Hippo

2019/4/10

#
Use a 50 instead of 10 for the cat and make sure you add the cat before you add the monsters and stars.
renniemm renniemm

2019/4/10

#
Thanks, it works!
You need to login to post a reply.