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

2024/7/15

How to slow down timer

Divyanshi Divyanshi

2024/7/15

#
I want to show my timer counting down 3 seconds but it goes too quickly how do I solve this? here's the code: public void waitFor(Actor sbob) { for(int i = 3; i >= 1; i--) { showText("Wait for: " + i, 400, 300); sbob.sleepFor(200); }
danpost danpost

2024/7/16

#
Divyanshi wrote...
I want to show my timer counting down 3 seconds but it goes too quickly how do I solve this? << Code Omitted >>
3 seconds is usually about the same amount of time for 180 to 200 act cycles to run in. You cannot program that in a for loop (or any other type of loop). The program must represent what the actor needs to do at the moment -- during that particular act step. You will need an int counter field to count the act steps:
private int waitTime = 0; // zero is not waiting

public void act() {
    if (waitTime > 0) { // waiting
        if (--waitTime > 0) { // continues waiting
            if (waitTime%60 == 59) { // time to show new text
                showText("Wait for "+(waitTime/60+1), 400, 300);
            }
        } else showText("", 400, 300); // remove text
        return; // do no more -- terminate execution of act method here
    }
    // rest of act
}

/**    SOMEWHERE IN ACT  (or in a method called by way of the act method)  */
if (...) waitTime = 180; // set waiting time (in act steps)
This might be close to what you want, but it shows how the field, along with the act method, can accomplish what you want.
Divyanshi Divyanshi

2024/7/18

#
thank you
You need to login to post a reply.