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

2017/11/12

Pls help me making a countdown

Troxter Troxter

2017/11/12

#
Hey, for my Scenario i will need some Countdowns that act like an delay (but without stopping the whole programm). So for example: I had 2 fish and the one eats the other. At that moment my Display should show the Message "RIP FISH" for 2 secounds. If i use greenfoot.delay(2) nothing else work at that moment. An other example: The escaping fish is able to pick up an upgrade that should make him invulnerable for 5 Sec. I found some post where they used greenfoot.setSpeet(50) and then declared an int time = <number>. I'm wondering if this could be a possible solution for my problem and if yes, what the numbers mean (ticks. secounds, millisecounds)
danpost danpost

2017/11/12

#
Troxter wrote...
I found some post where they used greenfoot.setSpeet(50) and then declared an int time = <number>. I'm wondering if this could be a possible solution for my problem and if yes, what the numbers mean (ticks. secounds, millisecounds)
It is a possible solution, and, in your cases, probably the best and easiest one. The timer is used to count act steps. Each step consists of your world and the active actors in that world having all their act methods executed. Greenfoot automatically buffers the gap between each step with a delay which is dependent on the speed of the scenario. The faster the speed -- the shorter the delay. As a side note, lagging occurs when the execution of a step exceeds the speed set for the scenario (or, rather, the time between steps based on the set speed). At any rate, at normal speed (50), the number of steps per second is about 60 (well, it used to be; I think it might be more once HTML 5 was introduced). Either way, you can always adjust the starting value of the timer to fit the situation.
Troxter Troxter

2017/11/12

#
danpost wrote...
Troxter wrote...
I found some post where they used greenfoot.setSpeet(50) and then declared an int time = <number>. I'm wondering if this could be a possible solution for my problem and if yes, what the numbers mean (ticks. secounds, millisecounds)
It is a possible solution, and, in your cases, probably the best and easiest one. The timer is used to count act steps. Each step consists of your world and the active actors in that world having all their act methods executed. Greenfoot automatically buffers the gap between each step with a delay which is dependent on the speed of the scenario. The faster the speed -- the shorter the delay. As a side note, lagging occurs when the execution of a step exceeds the speed set for the scenario (or, rather, the time between steps based on the set speed). At any rate, at normal speed (50), the number of steps per second is about 60 (well, it used to be; I think it might be more once HTML 5 was introduced). Either way, you can always adjust the starting value of the timer to fit the situation.
Thank you very much for the quick awnser. I have a new Problem now. I wanted to make the fish2 invulnerable for 5 secounds but he isnt and i am not sure why. I show you my code.
//fish2.class

    public static int e = 0;
    private int time = 300;

    public static int getE(){
       return e;
    }

    public void pkugel(){  //this is the powerup
        if (isTouching(pkugel.class)){
            removeTouching(pkugel.class);
            e++;
        }
    }

    public void essen(){
        if(e==1){
            Greenfoot.setSpeed(50);
            while(time>0){
                time--;
            }
            e--;
        }
    }

    public void act()
    {
        maus();
        pkugel();
        essen();
    }
//this is the fish3.class (the hunter)

    public void essen(){
        if(isTouching(fish2.class) && fish2.e==0)
        {
            removeTouching(fish2.class);
            if(i>6){
                i=6;
            }
        }   
    }
Troxter Troxter

2017/11/12

#
ok i replaced the method "essen" with the new one called powerUpTime and now it works. Pls explain me why it works now.
    public void powerUpTime(){
        if(e==1){
            time--;
            if(time==0){
                e--;
                time=300;
            }
        }
    }
danpost danpost

2017/11/12

#
Lines 20 through 22 instantly depletes the timer value. Using 'while' there prevents the timer from counting act steps. The setting of the speed of the scenario should be done at the time the world is being created (in your world constructor). It may not (or it may) matter that all fish2 objects will be invulnerable at the same time due to 'e' being static. It may not be necessary to have the 'e' field as all it does is inform that the timer value is not zero (which can be checked directly). The less fields you use, the less complicated, less confusing and less error-prone your code will be. Your code in the fish2 class might look more like this, then:
if (timer > 0) timer--;
and the code in the fish3 class like this:
fish2 f2 = (fish2) getOneIntersectingObject(fish2.class);
if (f2 != null && f2.timer == 0)
{
    getWorld().removeObject(f2);
    // etc.
No static content needed this way. The timer value should be initially zero and 'e++;' should be replaced with 'timer = 300;'.
Troxter Troxter

2017/11/12

#
danpost wrote...
Lines 20 through 22 instantly depletes the timer value. Using 'while' there prevents the timer from counting act steps. The setting of the speed of the scenario should be done at the time the world is being created (in your world constructor). It may not (or it may) matter that all fish2 objects will be invulnerable at the same time due to 'e' being static. It may not be necessary to have the 'e' field as all it does is inform that the timer value is not zero (which can be checked directly). The less fields you use, the less complicated, less confusing and less error-prone your code will be. Your code in the fish2 class might look more like this, then:
if (timer > 0) timer--;
and the code in the fish3 class like this:
fish2 f2 = (fish2) getOneIntersectingObject(fish2.class);
if (f2 != null && f2.timer == 0)
{
    getWorld().removeObject(f2);
    // etc.
No static content needed this way. The timer value should be initially zero and 'e++;' should be replaced with 'timer = 300;'.
Thank you!
You need to login to post a reply.