Table of Contents
Tutorial: Class Level and World Level Methods
Loading the World
Download the file that we'll be working with today. Save it in a directory that you can find again, and then start Alice and open the world.
First: After you have opened the file, and set up your world, go into the “Layout” mode by clicking on the green button 'Add Objects' (toward the middle of screen)
Click 'more controls.' Click 'drop a dummy at the camera.' Rename the dummy 'originalPosition.' To leave the layout mode, click 'done' This is something you should always do when you make a world in case you need to return the camera to this view later.
Part 1: A method is:
A method is a sequence of instructions that will be carried out when requested. Built in methods are primitive instructions every character (object) already knows how to perform. You use them to create new methods so that the characters (objects) can do more. The two types of methods are class-level and world-level.
Part 2: Class-level methods
A class-level method defines the behavior for a single character (object).
Step 1: How to create a class-level method:
- In our example world, click on the turtle in the object tree (upper left panel).
- In turtle's details (lower left panel) click the method tab and then the button 'create new method'. Name it 'walk'
Step 2: How to write a method:
- We want to move the turtle's legs back and forth as the turtle moves forward. First, drag the control statement 'do in order' from the bottom of the window, into the editor.
- Next, in the object tree, click on the + beside 'turtle' to see the different body parts. Drag the frontLeftLeg into the method. Select turn - backward. Choose other and type in 0.1.
- Finally, click 'more' at the end of the turn command and choose duration = .25 seconds. The default time for an action to be performed is 1 second. We are changing it to 0.25 so that it will happen faster.
- Next, we want the turtle to move forward at the same time that the back leg goes forward So drag in the control statement 'do together.'
- Finish dragging and dropping the instructions until your method looks like this:
- Finally, add a comment to your method to tell someone reading your code what it does. Your comment can say: “Move the turtles legs back and forth”
- Remember: This comment is not Alice code. It is simply an explanation for someone trying to understand your code. Alice ignores the comments when it plays your world.
Step 3: To call your method:
- Click on world.myfirstmethod to get back to it and then drag turtle.walk into it. Push the 'play' button to watch the turtle walk.
Step 4: Writing a method for kangaroo:
- In the object tree, click on kangaroo. In kangaroo's details, click the button 'create new method' and name it 'hop'
- To write the method, drag and drop the following instructions into your method. Your first step is to drag a 'do together' into the method.
- To find the 'lowerleg' of the kangaroo, you click the + beside the leg tab. When you finish, your method should look like this:
Part One of the Code:
Part Two of the Code:
- Remember, to call your method to test it, click on world.myfirst method to get back to it and then delete what you have there. Drag kangaroo.hop into it. Push the 'play' button to watch the kangaroo hop.
Part 3: World-level methods:
A world-level method has characters (objects) that interact with each other.
Step 5: Writing a world-level method:
- In our example, we are going to write a method so that the turtle and the kangaroo race each other.
- Click on 'world' in the object tree. Click on the methods tab and the button 'create new method.' Name it 'race'.
- First, we want the turtle to have a conversation with the kangaroo. The first step is to drag a 'do in order' into the method.
- Then, click on turtle in the objects area and drag it into the method.
- Put the following code into this method:
- Since we want the turtle and the kangaroo to move together, drag the control statement 'do together' into the race method.
- Click on turtle in the object area and click on method tab.
- Then drag turtle.walk into the 'do together'.
- Click on kangaroo in the object area.
- Drag kangaroo.hop into the 'do together' under turtle.walk.
- Click on world.myFirstMethod. Delete what you have there.
- Click on 'world' in the object area and drag world.race into myFirstMethod. Push play.
- The two characters only move once. If we repeatedly call the hop and walk methods, it will look more like a race.
- Instead of dragging the instructions over multiple times, let's use a loop.
Step 6: Loops:
- One of the control statements is 'Loop'. Drag Loop from teh bottom of the window, into the world.race method right above the 'do together'. Select other and type in 4.
- Drag the 'do together' statement (with kangaroo.hop and turtle.walk) into the Loop statement.
- Your code should look like this now:
- When you push play, the Kangaroo will win the race.
Save this world. We may come back to it later.
Recap
- This tutorial introduced class-level and world-level methods.
- If there is only one object in a method, it should be a class-level method.
- If there is more than one object involved, write a world-level method.
- To repeat an action, use a loop.