Terminology The instruction move() is called a method call. So the act method is executed over and over again, until we click Pause. Clicking the Run button is just like clicking the Act button many, many times, very quickly. That is, the instruction that we have written inside the act method (move()) executes. When we click the Act button in the Greenfoot main window, the act method is executed once. The move() instruction makes the crab move a little bit to the right. You will see that the crab can now move across the screen. The action is defined by a method of the object. What do you observe? Concept: A method call is an instruction that tells an object to perform an action. Exercise 2.3 Place multiple crabs into the world. Compile the scenario (by clicking the Compile All button) and place a crab into the world. The act method should then look like this: public void act() move() Exercise 2.2 Change the act method in your crab class to include the move() instruction, as shown above. One such command is move() Ĥ 36 Introduction to Programming with Greenfoot Note that it has to be written exactly as shown, including the parentheses and the semicolon. We can replace the grey text in the middle with a command. Here, we can add some code that determines the actions of the crab. The last three lines the two curly brackets and anything between them is called the body of the method. The first line is the signature of the method. It looks like this: public void act() // Add your action code here. Within this class definition, we can see what is called the act method. For now, we will concentrate on getting the crab to move. We will look at it in detail a little later. That is: this text defines what the crab can do. Code 2-1: The original version of the!crab" class This is a standard Java class definition. */ public class Crab extends Animal public void act() // Add your action code here. import greenfoot.* // (World, Actor, GreenfootImage, and Greenfoot) /** * This class defines a crab. (You can do this by selecting the Open editor function from the class s popup menu, or you can just double-click the class.) The source code you see is shown in Code 2-1. Open the editor to display the Crab source. The first thing we shall do is to make the crab move.ģ Chapter 2! The first program: Little Crab Making the crab move Let us have a look at the source code of class Crab. In this chapter, we shall work on changing this. This is because there is no source code in the definition of the Crab class that specifies what the crab should do. The crab does not do anything when Greenfoot runs. If you have just done the exercise above, then you know the answer to the What do you observe -question. We will talk a little more about the Actor and Animal classes later on. (It follows then, that a crab also is an actor.) Initially, we will work only with the Crab class. The hierarchy (denoted by the arrows) indicates an is-a relationship (also called inheritance): A crab is an animal, and an animal is an actor. We notice that there is the usual Greenfoot Actor class, a class called Animal, and the Crab class. What do you observe? (Remember: If the class icons on the right appear striped, you have to compile the project first.) Figure 2-2: The Little Crab actor classes On the right, you see the classes in this scenario (Figure 2-2). Place a crab into the world and run the program (click the Run button). Exercise 2.1 Start Greenfoot and open the little-crab scenario. The scenario you see should look similar to Figure 2-1. You will find this scenario in the book projects on the CD of this book. Figure 2-1: The Little Crab scenarioĢ 34 Introduction to Programming with Greenfoot 2.1 The Little Crab scenario The scenario we use for this chapter is called little-crab. Now, we want to start to make our own game. 1 2 The first program: Little Crab topics: concepts: writing code: movement, turning, reacting to the screen edges source code, method call, parameter, sequence, if statement In the previous chapter, we discussed how to use existing Greenfoot scenarios: We have created objects, invoked methods, and played a game.
0 Comments
Leave a Reply. |