Greenfoot sounds play intermittantly3/10/2024 Invoking act is essentially giving the object the instruction “Do whatever you want to do now”. What does this do?.Ģ3 Act() method All objects in a Greenfoot world have this act method. With a wombat and some leaves in the world, click the Act button in the execution controls near the bottom of the Greenfoot window. What does this method do? How does it differ from the move method? Make sure to try different situations, for example, the wombat facing the edge of the world, or sitting on a leaf. Place a wombat and a good number of leaves into the world, and then invoke a wombat’s act() method several times. Here is an example of a non integer value entered into the setDirection() method. If we insert a value greater than 3 there is no effect since the parameter we enter must be between or including 0 – 3. If there is anything within the parenthesis, then the method expects one or more parameters-additional information that we need to provide.ġ6 Parameters Here we are setting the direction in which the Wombat is facing.ġ9 Numbers indicating direction of the Wombat as the computer understands directions In other words, it expects no parameters-when we invoke the method it will just run. If we see only a pair of parentheses without anything else within it (as we have in all methods so far), then the method has an empty parameter list. This tells us whether the method requires any additional information to run, and if so, what kind of information. int getLeavesEaten() void setDirection(int direction) The parentheses after the method name hold the parameter list. When the wombat eats a leaf the int (integer) will increase to 1.ġ5 Parameters The other bit in the method menu that we have not yet discussed are the parentheses after the method name. This means the wombat can move forward.ġ3 This example shows the canMove() method call returning false since the wombat cannot move since it is at the edge of the game world.ġ4 Here we have the getLeavesEaten() method returning 0 since the wombat has not eaten any leaves. In this case the methods is telling us that the wombat can move if we want it to move.ġ2 Here we see the Greenfoot interface with the method canMove() returning true. This example shows a return type of true, which is a boolean return type. The return type of a method specifies what a method call will return. They just carry out their action, and then stop.ġ1 The return type of a method specifies what a method call will return The word void means “nothing” in this context: Methods with a void return type do not return any information. It tells us what the method returns to us when we invoke it. Right Click on the Wombat Invoke the Move methodġ0 Return Types The word at the beginning is called the return type. Interact with objects through the contextual menu by right clicking on the object to see what methods can be called. The game world for the wombat is part of the world class.Ĩ Make three wombats and three leaves as shown here. A wombat is an object of the Actor class. We can invoke a method by selecting it from the menu.ħ Objects and Classes Right Click on Wombat Drag to World Click New Wombat() This is how to make a wombat appear in the game world. It cannot hurt to get used to standard terminology straightaway, so we shall also call them methods from now on. In Java, these operations are called methods. For example, a wombat’s object menu shows us what this wombat can do (plus two additional functions, Inspect and Remove, which we shall discuss later). The object menu shows us all the operations this specific object can perform. This will pop up the object menu (Figure 1.4). (Objects are also often referred to as instances in programming-the two terms are synonyms.)Ħ Objects and Classes Once we have placed some objects into the world, we can interact with these objects by rightclicking them. Once we have a class in Greenfoot, we can create objects. The class Wombat stands for the general concept of a wombat-it describes all wombats. Let us start by looking at the Wombat class. The concepts of classes and objects are fundamental in object orientation. We shall be using the Java programming language for our projects. In this case, they are World, WombatWorld, Actor, Leaf, and Wombat. The class diagram shows us the classes involved in this scenario. We’ll come back to them in a little while, too.ĥ Objects and Classes We shall discuss the class diagram first. The Act, Run, and Reset buttons and the speed slider at the bottom are the execution controls. We shall discuss this in more detail shortly. The area on the right with the beige-colored boxes and arrows is the class diagram. This is where the program will run and we will see things happen. The largest area covering most of the screen (a sand-colored grid in this case) is called the world.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |