Creating Functions Tutorial
By Deborah Nelson
Duke University, July 22, 2008
Under the direction of Professor Susan Rodger
As you follow the instructions below, click on an image to enlarge it.
Then use the'go back one page' arrow in your web browser to come back to
this tutorial.
Loading the World
For this tutorial, download the starter world
here
Remember to save it in a directory that you can find again, and then start Alice and open the
world. NOTE: You cannot double-click the file to open it; Windows will not know what to
use, and even if you select Alice from a list of programs, the loading will fail.
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. If you don't understand dummys,look at the camera control tutorial
again.
Play your world
- In this world, the user is supposed to burn everything that is in the junkyard.
- Play the world
- There is an error. Because whatever the user clicks on burns – even the animals and the ground.
- We only want the things in the junkyard to be burned. So we need to write a function
Part 1: Functions
- Information about the world or the objects in the world are all stored in properties
- A function is used to ask questions about these properties
- For example, some of the built in functions of an object are: color, height and depth.
- A function is not a behavior or an action. It simply returns the information that we need.
- In this world, we want to know: is the object that was clicked on one of the objects in the junkyard?
- If it is one of the objects in the junkyard, we want the fire to go to it.
- If it isn’t, we don’t want anything to happen
Types of Functions
- Depending on the question, a function can return any type of value.
- Some of the options of a return type are:
- Number
- Boolean (true or false)
- Object
- String
- But there are more types
Part 2: creating a function
Step 1: What type of function to create
- We want our function to be type boolean. Boolean returns either true or false.
- This function will return true if the object clicked on is in the junkyard.
It will return false otherwise
Step 2: How to create a function
- Click on the function tab in the world detail’s pane.
- Click on create new function
- Name it possibleObject and make sure it is type boolean
Part 3: Writing the function
Step 1: The parameter
- The object that was clicked on is going to be used in this function. In the function,
create new parameter
- Name it possibleFuel and it is type object
Step 2: The if/else
- Drag an if/else into your function and select true in the drop down menu
- Drag the parameter possibleFuel on top of the true
Step 3: The conditional
- When you release the parameter, select possibleFuel ==;
- then select cardboardBox,
- the entire cardboardBox
The Return
- Drag return on top of the Do Nothing
- Select true
Nested if statements
- Drag if/else on top of the Do Nothing underneath Else
- Then drag possibleFuel on top of the true
- When you release it, select possibleFuel==;
- Select crate
- Select the entire crate
- Then drag return true onto the Do Nothing.
- Your function should look like this so far:
Step 6: Finish the function
- Drag a new if/else onto the Do Nothing underneath Else
- Repeat the steps for these objects:
- the teddyBear
- the table
- The oldBed
- Here is the rest of the function:
- Don't forget to set the final return to false
Part 4: Calling the function
- Click on the methods tab in the world details pane
- Click edit beside the method lite so that you can see the code
- Click on the functions tab in the world details pane
- Drag if/else to the top of the lite method
- Now drag the possibleObject from the function tab on top of the true
- Select expressions
- Select objClick
-
Click on the Do in Order
- Drag it into the if/else statement on top of the Do Nothing
- Play your world
- Scroll down to the Else and drag the camera say method and type in the string “that object shouldn’t be burned”:
Part 5: One more error
- Now when you play your world, only the objects that are in the junkyard are burned.
- However, when you burn one of the objects, you can still burn it again
- Let’s nest another if statement in the lite method to check whether or not the object has
been burned
- "Burned" means its color has been changed to black
Step 1: Writing the new function: is it burned?
- Now, click on the function tab in the world details pane.
- Create a new function named notBurned. It should also be type Boolean
- Drag an if/else into the function
- In this function, we want to return true if the object has not been burned
- We know an object has been burned if it’s black
- Click on cardboardBox in the object tree
- Click on it’s properties tab
- Drag color on top of the true in this function
- Select != black.
Step 2: Return statements
- Now drag return on top of the first Do Nothing. Select true
- Drag return on top of the second Do Nothing. Select false. Select false for the
last return
- Complete method:
Step 3: Create a new parameter
- Create a new parameter in the function
- Name it objClicked and it is type object
- Drag objClicked on top of cardboadBox in the function’s if statement
Step 4: Nest an if/else
- Drag in a new if statement
- For the Else of that statement, drag in the camera say method and type in: “that object
has already been burned”
- So far, your method should look like this:
- Now click on the Do in order and drag it on to of the Do Nothing underneath If
Part 6: Calling the function notBurned
- Click on the world.lite tab so that you can see the code
- Click on world in the object tree. Click on the functions tab. Drag the function notBurned on top of
the true in your if statement
- Then drag the parameter objClick onto the
Play your world
Recap
- A function is not a behavior or action
- It is used to return information about the state of the world or the characters in the
world
- There are many different types of functions. For example, functions can return objects,
numbers, booleans, etc
- Write your own functions based on questions you need to answer about your world