//new to java /
Greenfoot.playSound("slurp.wav");
placeNewPizza();
The placeNewPizza() method will place
a new slice into the world every time a
slice gets eaten. This method does not
exist—we will have to write it now.
The full code for this method is shown
in Listing 1. We see the eat() method that
calls the placeNewPizza() method and
the definition for this new method. Let’s
analyze this line by line.
To place a new pizza slice at a random location into the world, we have to
achieve three things:
; ; Create a new pizza object.
; ; Generate random coordinates.
; ; Place the pizza at those coordinates
into the world.
The first line in our new method reads
We have seen method calls to the
getRandomNumber, get Width, and
getHeight methods before. Here, we
generate a random number between
0 and the world’s width and store it in
the x variable. Then we do the same
for y using the height of the world as
the limit. This gives us random x and y
coordinates that match the exact size of
the world.
/**
Look for pizza and eat it if we see some.
*/
public void eat()
{
Actor pizza = getOneIntersectingObject( Pizza.class);
if(pizza != null) {
getWorld().removeObject(pizza);
Greenfoot.playSound("slurp.wav");
placeNewPizza();
}
}
LISTING 1
COMMUNITY
JAVA IN ACTION
my World.addObject(new Pizza(), x,
y);
World my World = get World();
We have seen before that the
get-World() call gives us a reference to our
current world object. This time, we
declare a local variable named my World
(it is of type World) and store the world
object in it. This, in itself, does not
achieve very much yet, apart from the
fact that we can now use the my World
variable to talk to our world.
In the next two lines, we generate the
random coordinates and store them in
two local integer variables:
Here, we place the new pizza object
into the world. To do this, we are using
the world’s addObject method. This
method expects three parameters: the
object to be placed, the x coordinate,
and the y coordinate.
/**
Place a new pizza slice at a random location into the world.
*/
public void placeNewPizza()
{
World my World = get World();
int x = Greenfoot.getRandomNumber(my World.get Width());
int y = Greenfoot.getRandomNumber(my World.getHeight());
my World.addObject(new Pizza(), x, y);
}
ABOUT US
Download all listings in this issue as text
This is what it looks like:
public class Turtle extends Actor
{
int x = Greenfoot.getRandomNumber
( myWorld.getWidth());
int y = Greenfoot.getRandomNumber
( myWorld.getHeight());
Counting Pizza
The functionality so far gives us an endless supply of pizza slices. The next task
to be done is to count how much we
have eaten. We do this by declaring a
field (also known as an instance variable)
named slices to use for the counting.
(We’re still working in the Turtle class.)
...
}
The field declaration consists of the
keyword private, followed by a type and
name for our variable, and an optional
initialization value. The type and name
(int slices) is the same as we have seen
for a local variable. The initialization (= 0)
assigns zero to the variable to start.
blog
22
ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2012