310 likes | 451 Views
Creating New Methods in Alice. Create a world. We will create a world with three objects from Lewis Carroll’s Alice’s Adventures in Wonderland: The Cheshire Cat The White Rabbit Alice We will write methods to make them jump up and down.
E N D
Create a world • We will create a world with three objects from Lewis Carroll’s Alice’s Adventures in Wonderland: • The Cheshire Cat • The White Rabbit • Alice • We will write methods to make them jump up and down. • Open a new world with a grass template and let’s begin.
CamelCase • Find the Cheshire Cat class in the Animals folder of the objects gallery. • Notice how the name of this class is constructed (CheshireCat). • The names of classes and objects in Alice follow a pattern called the CamelCase naming convention. • CamelCase is used and recommended by many software developers (e.g., Microsoft).
CamelCase, cont’d • CamelCase is the practice of writing compound names without using blank spaces, but capitalizing the first letter of each name that forms the compound name. • In Alice in Wonderland, the proper name of the character is Cheshire Cat. • In CamelCase, we write CheshireCat.
Using CamelCase • We will use this convention for all classes. • We the same convention with one difference for all: • Objects • Methods • Parameters • Variables • For these we do not capitalize the first letter • E.g., the object is named, cheshireCat.
Editing our initial scene 1. Add instances of the following classes: • CheshireCat • WhiteRabbit • AliceLiddell
Editing, cont’d 2. Arrange the characters as seen below:
Editing, cont’d 3. Make each character face the camera.
Editing, cont’d • Exit Scene Editor Mode
Designing a method • The design of a new method begins with specifications. • In the simple case of this tutorial, the specifications are given: • The three characters appear on a grass background. • aliceLiddell jumps • whiteRabbit jumps • cheshireCat jumps
Organizational Chart Triple Jump aliceLiddell jump whiteRabbit jump cheshireCat jump
Program outline Triple Jump Program • aliceLiddell jump • whiteRabbit jump • cheshireCat jump
Expanded outline Triple Jump Program • aliceLiddell jump • aliceLiddell move up • aliceLiddell move down • whiteRabbit jump • whiteRabbit move up • whiteRabbit move down • cheshireCat jump • cheshireCat move up • cheshireCat move down
Coding the jump methods • Top-down design and modular development suggest that separate jump methods should be written for each object. • To make an object jump, we’re going to move the character up one meter and then back down one meter.
Create a jump method for whiteRabbit • Click the whiteRabbit tile in the Object Tree. • Make sure the methods tab in the Details Area is displayed. • Click the create new method button. • Type jump as the name for the new method and click OK.
Used for methods that require some input data Name of the method Method header Instruction zone
Coding whiteRabbit.jump • Your method should include an instruction to make whiteRabbit move up one meter followed by an instruction to make it move down one meter. • As defined by our specification
Coding whiteRabbit.jump, cont’d 1. Drag the whiteRabbit move tile from the left side of the screen and drop it on the Do Nothing phrase. Note: The tile will be surrounded by a red border as you drag it. The border will turn green when you reach the right place.
Coding whiteRabbit.jump, cont’d • When you drop the tile into the instruction zone, a menu will appear asking you the direction in when you want to move the object. Select up for the direction. Select 1 meter for the amount. Note: Direction and amount are parameters or input data for the move method.
Coding whiteRabbit.jump, cont’d 3.Drag another copy of the whiteRabbit move tile from the left into the instruction zone below your first instruction. This time set the direction to down, and the distance again to 1 meter.
Creating the other methods • Select the cheshireCat tile in the Object Tree, and make sure its methods are displayed in the Details Area. • Click the create new method button, and then name the method jump. • Add an instruction to make cheshireCat move up 1 meter. • Add an instruction to make cheshireCat move down 1 meter. • Create a jump method for aliceLiddell in a similar manner.
Creating the main method • Now we need a method that calls all three jump methods in the order defined by our specification. • This method will be your main method at the top of the organizational chart. • World.my first method is often used as this top-level method. We’ll do that here. • This a world-level method, associated with the world itself rather than with any particular object.
Creating the main method 1. Start by clicking the world tile in the Object tree, and then click the methods tab, if necessary. Note: This looks the same as for an object’s methods.
Creating the main method • Locate the my first method tile and click the edit button. • Click the aliceLiddell tile in the Object tree and find her methods. • Click the jump tile and drage a copy of it into the instruction zone of world.my first method • Add a call to the jump methods for whiteRabbit and cheshireCat in the same way.
Testing and Debugging • Click the Play button. • Watch what happens. Did it perform according to your original specifications? • Save your world as tripleJump.
Debugging • Each method should perform a single task. • If an object is doing something wrong, then you should ask which of your methods performs that task. • If you have an error, then it could be either the main method or one of the jump methods. • If the three objects are jumping in the wrong order then main method must be coded incorrectly.
Generic methods and parameters • Let’s create a generic jump method that will work for any object. • To do this we need to use variable parameters. • A variable parameter is a name for a memory location that temporarily stores a value while a method is running.
Variables • Variables are a lot like the properties of objects. • They have data types, just like properties. • They are stored in the memory of a computer, just like properties. • However, properties are associated with an object, and their values are maintained as log as the object exists. • Variables are associated with a particular method, and their values exist only inside the method. • Once the method stops running their values are gone.
Parameters • A parameter is a variable whose value is passed from one method to another. • Not unlike a baton in a relay race. • We had to specify the direction and amount to move in the call to cheshireCat.move. • The values are parameters for the primitive cheshireCat.move method. • Our cheshireCat.move method passed the values for the params when calling move.