Code Clip 1:
Paste in the Water class inside the prepare() method below the line
addObject(fish, 303, 213);
Fly[] flies = new Fly[5];
for(int i=0;i<flies.length;i++)
{
flies[i] = new Fly();
int flyX = Greenfoot.getRandomNumber(getWidth());
int flyY = Greenfoot.getRandomNumber(getHeight());
addObject(flies[i], flyX, flyY);
}
Code Clip 2:
Paste in the Fly class below the opening curly brackets under public class Fly extends Actor {
int rightSideOfScreen;
int bottomOfScreen;
public void addedToWorld(World water)
{
rightSideOfScreen = water.getWidth() - 1;
bottomOfScreen = water.getHeight() - 1;
}
Code Clip 3:
Paste in the act() method of the Fly class
move(10);
if(Greenfoot.getRandomNumber(20)<2)
{
setRotation(Greenfoot.getRandomNumber(359));
}
int x = getX();
int y = getY();
if(x<=0 || y <= 0 || x >= rightSideOfScreen || y >= bottomOfScreen)
{
turn(180);
}
First, open the Water world code window
Inside the prepare() method below the line
addObject(fish, 303, 213); remove the comment and paste code clip 1.
Close the Water world code window and open the Fly actor code window
Under the opening curly brackets under the public class Fly extends Actor {
paste Code Clip 2.
Inside the act() method of the Fly class paste Code Clip 3.
Close the window and compile. Click 'Run' and watch the flies move randomly.
Code clip 1 contains the lines:
Fly[] flies = new Fly[5];
for(int i=0;i<flies.length;i++)
{
flies[i] = new Fly();
int flyX = Greenfoot.getRandomNumber(getWidth());
int flyY = Greenfoot.getRandomNumber(getHeight());
addObject(flies[i], flyX, flyY);
}
This creates an Array variable, which is a kind of variable that can
hold multiple objects, in this case 5. To get more or less flies, change the 5.
The for loop begins at 0 and counts up to the number of objects in the flies array, defined
by flies.length.
The line flies[i] = new Fly creates a new Fly in the i-th instance of
the fly array. The next two lines generate random x and y coordinates for the new fly
using the Greenfoot.getRandomNumber() function. The maximum
number for the x and y come from the getWidth() and
getHeight() methods. Then the flies are added to the world using
addObject();
Code clip 2 contains the lines:
int rightSideOfScreen;
int bottomOfScreen;
public void addedToWorld(World water)
{
rightSideOfScreen = water.getWidth() - 1;
bottomOfScreen = water.getHeight() - 1;
}
This line creates integer variables that will hold the coordinates of the right and
bottom side of the screen. The
addedToWorld() method is
automatically called when the flies are added. The world is assigned to the variable
water, which is then used to get the width and height of
the world and assign it to the variables.
By assigning the variables in the
addedToWorld() method we avoid calling the world before
we're added to it, which will give us a Null Pointer Exception error.
The first lines of Code Clip 3 are:
move(10);
if(Greenfoot.getRandomNumber(20)<2)
{
setRotation(Greenfoot.getRandomNumber(359));
}
The
move() method simply moves the fly forward; change
the
(10) to
a bigger or smaller number to speed it up or slow it down. The conditional takes a
random number from 1 to 20, and if it is less than two (i.e. exactly 1) it runs the
code inside the curly brackets. In other words, it has a 1 in 20 chance every
turn to change the rotation. To make the fly turn more or less often, change the
(20) to a bigger or smaller number.
The setRotation() method picks a random number from 1 to 359
and assigns it to the direction of the fly. You can try replacing this with a line such as
turn(Greenfoot.getRandomNumber(90)); which will make the fly turn
a smaller amount.
The next part of Code Clip 3 contains the lines:
int x = getX();
int y = getY();
if(x<=0 || y <= 0 || x >= rightSideOfScreen || y >= bottomOfScreen)
{
turn(180);
}
The first two lines just assign the coordinates of the fly,
acquired by the
getX() and
getY()
methods, to the integer variables x and y.
The conditional checks to see if the x and y
coordinates are less than or equal to 0 (going off the top or left of
screen) or greater or equal to the rightSideOfScreen
and bottomOfScreen variables assigned in Code Clip 2. If so, it turns
the fly around 180 degrees using the turn() method.