Creating Scenarios In Greenfoot

Preview:

DESCRIPTION

Creating Scenarios In Greenfoot. Greenfoot Scenarios are made up of: A World class. Actor classes. To add these in a new scenario: Right click on the World class or Actor class Select “New Subclass”. Select the background you want (for a World) or the icon your want (for an Actor). - PowerPoint PPT Presentation

Citation preview

Creating ScenariosCreating ScenariosInIn

GreenfootGreenfoot

Greenfoot Scenarios are made up of:Greenfoot Scenarios are made up of:

A World class.A World class.

Actor classes.Actor classes.

To add these in a new scenario:To add these in a new scenario: Right click on the World class or Actor classRight click on the World class or Actor class Select “New Subclass”Select “New Subclass”

Select the background you want (for a World) Select the background you want (for a World) or the icon your want (for an Actor)or the icon your want (for an Actor)

Greenfoot Worlds are Greenfoot Worlds are composed of a grid.composed of a grid.

Grids are composed of Grids are composed of cells. For example, this cells. For example, this grid is 3 cells by 4 cells.grid is 3 cells by 4 cells.

The cells in Greenfoot The cells in Greenfoot are all square.are all square.

To define the properties of a World’s grid, you To define the properties of a World’s grid, you put the following in the World’s constructor:put the following in the World’s constructor:

super(10, 5, 10);super(10, 5, 10);

The World is 10 cells wide.

The World is 5 cells high.

Each cell is 10 pixels by 10

pixels.

super(10, 5, 10);super(10, 5, 10);

If your cells are really big, the animation for your actors will If your cells are really big, the animation for your actors will look choppy. The actors are going from one cell to the next.look choppy. The actors are going from one cell to the next.

If your cells are really big, the animation for your actors will If your cells are really big, the animation for your actors will look choppy. The actors are going from one cell to the next.look choppy. The actors are going from one cell to the next.

If your cells are really big, the animation for your actors will If your cells are really big, the animation for your actors will look choppy. The actors are going from one cell to the next.look choppy. The actors are going from one cell to the next.

The solution to get rid of the choppy animation The solution to get rid of the choppy animation is to shrink the cell size.is to shrink the cell size.

super(10, 5, 1);super(10, 5, 1);

The World is 10 cells wide.

The World is 5 cells high.

Each cell is 1 pixel by 1

pixel.

super(10, 5, 10);super(10, 5, 10);

Notice that the world is now smaller because our cell size is smaller. We need to adjust the dimensions of the world to account for the smaller cell size.

Let’s double the width and height of the Let’s double the width and height of the world’s cells:world’s cells:

super(20, 10, 1);super(20, 10, 1);

The World is now 20 cells

wide.

The World is now 10 cells

high.

Each cell is 1 pixel by 1

pixel.

super(20, 10, 1); super(20, 10, 1);

Now our animation should be smoother.Now our animation should be smoother.

super(20, 10, 1); super(20, 10, 1);

Now our animation should be smoother.Now our animation should be smoother.

super(20, 10, 1); super(20, 10, 1);

Now our animation should be smoother.Now our animation should be smoother.

The World subclass that you create has access to the The World subclass that you create has access to the World class’ methods.World class’ methods. One important method is addObject( )One important method is addObject( )

addObject( )addObject( ) To automatically create an Actor method in the world when To automatically create an Actor method in the world when

your World class is initialized use this method.your World class is initialized use this method.

Syntax: Syntax: addObjectaddObject(greenfoot.Actor object, int x, int y) (greenfoot.Actor object, int x, int y)

Ex: addObject(new Ladybug( ), 7, 3); Ex: addObject(new Ladybug( ), 7, 3);

addObject(new Ladybug( ), 7, 3); addObject(new Ladybug( ), 7, 3); The Ladybug will be created here. The Ladybug will be created here. You have probably noticed that Java’s y axis starts at the top You have probably noticed that Java’s y axis starts at the top

left!left!

The Actor subclass (Ladybug) that you create has The Actor subclass (Ladybug) that you create has access to the Actor class’ methods.access to the Actor class’ methods. One important method is setLocation( )One important method is setLocation( )

setLocation( )setLocation( ) To send an Actor object to a location in the grid you use To send an Actor object to a location in the grid you use

this method.this method.

Syntax: Syntax: setLocationsetLocation(int x, int y) (int x, int y)

Ex: setLocation(2, 4); Ex: setLocation(2, 4);

setLocation(2, 4); setLocation(2, 4); The Ladybug will be sent here. The Ladybug will be sent here.

The Actor class also has “get axis” methods that The Actor class also has “get axis” methods that return the location of the object.return the location of the object. getX( )getX( ) getY( )getY( )

To make the Actor object move forward use these To make the Actor object move forward use these methods with the setLocation method inside the Act methods with the setLocation method inside the Act method:method:

Ex: setLocation(getX( ) + 1, getY( )); Ex: setLocation(getX( ) + 1, getY( ));