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

2019/3/14

When using a while loop, it doesn't use any of the wait variables i've used for delay. It just skips it all.

TheKgroup TheKgroup

2019/3/14

#
private int countdowntime = 4;
    private int countdowndelay = 0;
    private int countdownreach = 250;
    private boolean wait = false;
    
    public void countdown()
    {
        while (countdowntime > 0)
        {
            wait = true;
            countdowndelay++;
            if (countdowndelay >= countdownreach)
            {
                countdowntime--;
                Greenfoot.playSound("Countdown.mp3");
                countdowndelay = 0;
                String text = "";
                text = Integer.toString(countdowntime);
                this.showText(text,this.getHeight()/2,this.getWidth()/2);
                if (countdowntime == 1)
                {
                    countdownreach = Greenfoot.getRandomNumber(250);
                    countdownreach = countdownreach+250;
                }
                if (countdowntime == 0)
                {
                    countdownreach = 250;
                }
            }
        }
        countdowntime = 4;
        started = true;
    }
Why does it skip it all?
TheKgroup TheKgroup

2019/3/14

#
For context if my wording was wrong, where it has all the delays, I'm assuming it skips it straight too the countdownreach and it reaches the goal instantly, so instead of having a like 1-3 second delay, it skips to 0.
danpost danpost

2019/3/14

#
TheKgroup wrote...
<< Code Omitted >> Why does it skip it all?
Nothing is skipped. A while loop (like a for or do loop) will execute until its completion within the act step. You are causing everything to be done at once by using the loop.
TheKgroup TheKgroup

2019/3/14

#
How would I be able to loop it until it gets to 0 without it going all at once? I want it to have a timer so it goes 3, 2, 1.
danpost danpost

2019/3/14

#
TheKgroup wrote...
How would I be able to loop it until it gets to 0 without it going all at once? I want it to have a timer so it goes 3, 2, 1.
You can add:
Greenfoot.delay(1);
to literally pause for a time frame. A simplified version would be like:
int cdTimer = 181;

public void countdown()
{
    while (cdTimer > 0)
    {
        if ((--cdTimer)%60 == 0)
        {
            Greenfoot.playSound("Countdown.mp3");
            shotText(""+(cdTimer/60+1), getWidth()/2, getHeight()/2);
            Greenfoot.delay(1);
        }
    }
    started = true;
}
Or, even better:
public void countdown()
{
    for (int i=3: i>0; i--)
    {
        Greenfoot.playSound("Countdown.mp3");
        showText(""+i, getWidth()/2, getHeight()/2);
        Greenfoot.delay(60);
    }
    started = true;
}
You need to login to post a reply.