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

2015/11/18

how to make greenfoot re-execute the code

axxv axxv

2015/11/18

#
if a boolean is false which makes the code not run, how can I make greenfoot re-execute the code until boolean is true
public void act() 
    {
        if(start <= randomNumber)
        {
            Greenfoot.start();
        	frameChange();
        	mouseClick();
        	delayTime();
    	}
    }    
    private int frame=1;
    private int imageDelay=0;
    private int timer;
    private int start = Greenfoot.getRandomNumber(15);
    private int randomNumber = Greenfoot.getRandomNumber(10);
danpost danpost

2015/11/18

#
I do not see how the given code applies to the question. The only boolean value in the code is the condition in line 3 which will be true to execute the block of code for the 'if' statement. In general, if you want code to continue to repeat until a specific condition changes, a 'do-while' or 'do-until' loop would be coded. The java tutorial has pages on how to use these.
Royalblue64 Royalblue64

2015/11/19

#
I believe the answer to your question is that you have to reset the values of "start" and "randomNumber" in the act method.
private boolean started = false;
private int frame=1;
private int imageDelay=0;
private int timer;
private int start = Greenfoot.getRandomNumber(15);
private int randomNumber = Greenfoot.getRandomNumber(10);

public void act()
{
	if (!started)
	{
		start = Greenfoot.getRandomNumber(15);
		randomNumber = Greenfoot.getRandomNumber(10);
		
		if (start <= randomNumber)
		{
			Greenfoot.start();
			frameChange();
			mouseClick();
			delayTime();
			started = true;
		}
	}
}
This will cause the program to make another attempt at starting each frame until it succeeds.
You need to login to post a reply.