Chapter 2: Making your own scenario

It is now time to make our own scenario using the Greenfoot tool.  At the top menu bar of Greenfoot, select Scenario->New.  You will then be prompted for a name and to browse for a folder to put it in.  If you have not created a folder of scenarios, I suggest you create one now.  You can navigate to the place you want to put your scenario folder and then click on the "New Folder" button in the lower left to make the folder.  It will ask you for a folder name.  Now fill in the file name on the top to create your scenario.

The resultant window is different than Greenfoot windows we have seen so far.  The window is small, there is no world space inside, and there are no Actor classes listed in on the right.  We need to create a world and give it a size and create one or more Actor classes.
Lets make the world first.  Right click (CTRL+click on a Mac) on the World class and select "New subclass".  You will be prompted for a name.  You can put anything here, for now I suggest you type in "BugWorld".  After you complete this the new world dialog window closes and nothing happens excpet you have "BugWorld" listed as a subclass of World.  Click on "compile all" on the bottom and you will see the space for the world create on the screen.  The space is smaller than we have used so far.  This is because the default is to create a world of 20x20 cells where each cell is 10 pixels x 10 pixels.  We can change this to whatever we want.  To change the world size, double click on "BugWorld", or whatever you named your World subclass.  In the code for your subclass you will see the code:
public BugWorld()
{    
// Create a new world with 20x20 cells with a cell size of 10x10 pixels.
super(20, 20, 10);
}
This method has the same name as the class.  Such a method is called a constructor.  It specifies how objects of this class should be constructed.  In this case all the constructor does is set the world to be 20 cells wide, 20 cells high, and the cell size to be 10.  Go ahead and change on of these values and re-compile to see the effect.  Once done change back to 20,20,10 and recompile.

 

The grid of cells is not visible on the screen, but it is there.  To "see" the grid we will make a sprite, put it in the world, and move it over counting the cells.  First we need to create an actor subclass.  Right click on the Actor class box and select "New subclass" from the menu.  A window pops up.  Set the new class name to LadyBug and then under image categories select Animals and then under Libary images scroll down and double click on a lady bug image.  You should now have an actor subclass labelled LadyBug listed on the right, but it should have slashed lines to indicate it needs to be compiled.  Click on the Compile All button to compile the class.  Now put  a lady bug sprite into the world but right-clicking on the LadyBug actor class and placing it in the world.  The lady bug looks very big in this small world.  Click on run.  Noting happens.

The reason nothing happens is the LadyBug class does not have any code inside it's act() method.  Double click on the LadyBug class to open up the code editor.  Look at the act() method: there is no code in it.  You can cut and past code from prevous scenarios and type in code yourself.  Change the act() method so it now contains:

 

public void act()
{
moveRightOne();
}
 

 

Go ahead and compile. it. It does not work! Greenfoot highlights the offending line of code in yellow and puts the following message at the bottom: "cannot find symbol - method moveRightOne()". This is because there is no method in the LadyBug actor class called moveRightOne, hence, we need to copy and past that into the class also. Go ahead and add the method below the act method. You code should now look like:

 public void act() 
{
moveRightOne(); 
} 
public void moveRightOne()
{
setLocation( getX() + 1 , getY() );           
} 

Now compile the code and run it by place a LadyBug in the world and clicking the act button. Each time you click on the act button the LadyBug act() method is called and the bug is move over one cell to the right. Move the lady bug as far to the left as possible. Now click the act button repeatedly counting each time the bug moves. You will see it moves 20 times, from cell 0 to cell 19.

 ----------- Start Exercise 2.1 ------------------------ 

Create a world with two different types of bugs. One type of bug moves back and forth left-right and the other moves back and forth up-down

 ----------- End Exercise 2.1 ------------------------ 

 ----------- Start Exercise 2.2 ------------------------ 

Create a world with four different types of bugs. One type of bug moves back and forth left-right, one moves back and forth up-down, one moves around in both directions, and the last is keyboard controlled.

 ----------- End Exercise 2.2 ------------------------ 

Advanced note: the line of code "super(20,20,10)" found in the constructor for BugWorld above is actually a call to the constructor of the base class "World".  Inside the World class you will see how the grid size is initialized.  We will come back to this later when we explain base/super and subclasses.  If this does not make sense now do not worry about it, unless you have already programmed in an OO language it should not make sense yet.