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

2016/12/5

Loop Help

Wasupmacuz Wasupmacuz

2016/12/5

#
I'm trying to get a Seagull to dive toward a beach every-so-often and keep flying after it gets back up but the loop isn't working
    public void act() 
    {
        if (Greenfoot.getRandomNumber(101)<5)
        {
            setRotation(90);
            while (twoSeventy==false)
            {
                turn(-1);
                move(2);
                if (getRotation()==270)
                {
                    twoSeventy=true;
                }
                else
                {
                    twoSeventy=false;
                }
                
            }
        }
        else
        {
            setRotation(0);
            move(3);
        }
    }    
Super_Hippo Super_Hippo

2016/12/5

#
I don't think that you want to use a while loop there. Just changing it to 'if' won't work either though. What exactly are you trying to achieve with line 3? Do you want it to turn after a random amount of time? Maybe like this?
private boolean turning = false;

public void act()
{
    if (!turning && Greenfoot.getRandomNumber(20) == 0)
    {
        setRotation(90);
        turning = true;
    }
    
    if (turning)
    {
        turn(-1);
        move(2);
        if (getRotation() == 270)
        {
            turning = false;
        }
    }
    else
    {
        setRotation(0);
        move(3);
    }
}
Wasupmacuz Wasupmacuz

2016/12/6

#
Super_Hippo wrote...
I don't think that you want to use a while loop there. Just changing it to 'if' won't work either though. What exactly are you trying to achieve with line 3? Do you want it to turn after a random amount of time? Maybe like this?
private boolean turning = false;

public void act()
{
    if (!turning && Greenfoot.getRandomNumber(20) == 0)
    {
        setRotation(90);
        turning = true;
    }
    
    if (turning)
    {
        turn(-1);
        move(2);
        if (getRotation() == 270)
        {
            turning = false;
        }
    }
    else
    {
        setRotation(0);
        move(3);
    }
}
Thank you so much! since I'm new to Greenfoot my post wasn't 'posted' so I figured what you said out while I was waiting (oops). Again thank you though
You need to login to post a reply.