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

2012/3/13

Something

ihatenoahbravo ihatenoahbravo

2012/3/13

#
how do you make an actor move randomly around the world?
Morran Morran

2012/3/13

#
There are two different ways to do this. One is the "purely" random way:
public void act()
{
   int xRand = speed - Greenfoot.getRandomNumber(speed*2);
   int yRand = speed - Greenfoot.getRandomNumber(speed*2);
   //where speed is an "int" that you defined as a variable
   setLocation(getX() + xRand, getY() + yRand);
}
The other makes the actor move in a seemingly smarter way. You have to have your Actor extend Greenfoot's Animal class and...
public void act()
{
   move();
   int rotRand = speed - Greenfoot.getRandomNumber(speed*2);
   //where again speed is some "int" that you have defined.
   setRotation(getRotation() + rotRand);
}
I hope this is what you are looking for.
ihatenoahbravo ihatenoahbravo

2012/3/14

#
Alright thank you
Duta Duta

2012/3/14

#
Morran wrote...
setRotation(getRotation() + rotRand);
Or
turn(rotRand);
You need to login to post a reply.