Creating Scenarios In Greenfoot

21
Creating Scenarios Creating Scenarios In In Greenfoot Greenfoot

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

Transcript of Creating Scenarios In Greenfoot

Page 1: Creating Scenarios In Greenfoot

Creating ScenariosCreating ScenariosInIn

GreenfootGreenfoot

Page 2: Creating Scenarios In Greenfoot

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”

Page 3: Creating Scenarios In Greenfoot
Page 4: Creating Scenarios In Greenfoot

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)

Page 5: Creating Scenarios In Greenfoot

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.

Page 6: Creating Scenarios In Greenfoot

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.

Page 7: Creating Scenarios In Greenfoot

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

Page 8: Creating Scenarios In Greenfoot

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.

Page 9: Creating Scenarios In Greenfoot

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.

Page 10: Creating Scenarios In Greenfoot

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.

Page 11: Creating Scenarios In Greenfoot

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.

Page 12: Creating Scenarios In Greenfoot

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.

Page 13: Creating Scenarios In Greenfoot

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.

Page 14: Creating Scenarios In Greenfoot

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

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

Page 15: Creating Scenarios In Greenfoot

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

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

Page 16: Creating Scenarios In Greenfoot

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

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

Page 17: Creating Scenarios In Greenfoot

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);

Page 18: Creating Scenarios In Greenfoot

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!

Page 19: Creating Scenarios In Greenfoot

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);

Page 20: Creating Scenarios In Greenfoot

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

Page 21: Creating Scenarios In Greenfoot

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( ));