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

2015/9/30

Help make an object go from one random point to original point

Vicidsmart Vicidsmart

2015/9/30

#
My request is a way to use the random number generator once at the beginning of the code. Currently I am trying to make an ant spawn at the antpile (this works) the go to a random point (works) then go back to the antpile(doesn't work) and delete itself(haven't got there yet) (unless it hits a food source.) I have a problem because I'm trying to create a random point for it to go to but it keeps randomly generating places for it to go to (in which it will spaz out.) I want it to go to a singular random point the go back the the antpile where it started at. If I create time and on the 2nd time step it creates the point for it go to I don't know how to make it remember when to stop and go back. Here is my code:
import greenfoot.*;


public class Ant extends Actor
{int time=0;
    
    public void act() 
    {
        time=time+1;
        move(4);
        if(time==1)
        {
            int x=Greenfoot.getRandomNumber(599);
            int y=Greenfoot.getRandomNumber(399);
            turnTowards(x,y);
        }
        //if(isAtEdge())
        //{
           //this doesn't work and I won't need it if my problem is solved 
            //turnTowards(Antpile.getX(),Antpile.getY());
        //}
    } 
}
danpost danpost

2015/9/30

#
You do not need to use 'time' to determine when to create the point location to move to. You even said you wanted to do that first thing:
Vicidsmart wrote...
My request is a way to use the random number generator once at the beginning of the code.
You can add a constructor method to generate the point. You can also add two 'int' fields to hold the destination location coordinates or one int field to count down the number of act cycles (or moves) it takes to reach that destination (now, 'time' can be used to compare to this value).
You need to login to post a reply.