MCS-170 The Nature of Computer Science
Alice Lecture 4
1. Parameters and Event Handling
- Overview
- Indefinite Loops
- Parameters
- Event Handling
- Indefinite Loops - The While
Statement
- The Loop statement (tile) is used to carry out a series of
actions a fixed numebr of
times. But, what do we do if we don't know how many times we want
to repeat an action?
- Consider our Eskimo girl and (cold) snake example from last
class. If the distance from the snake to the girl is 20 meters,
the Loop will repeat the move
method exactly 20 times and then stop. But, suppose we want to be
able to move the girl around and have the snake follow, for an
indefinite time, until the snake eventually catches the girl. The
While
statement will allow us to create this action.
- The way the While statement works is as follows:
- While (some condition is true) do (set of actions)
- Example:
EskimoGirl and snake (version 2)
[ Girl is movable through the use of an Event - more on Events later]
- Parameters
- Recall that in Javascript, a procedure like prompt took parameters,
values or variables that were passed into the function. Ex:
- prompt("Enter a number:", "1");
- parameters here are the two strings that get passed into the
procedure prompt
- In Javascript, parameters could only take on three types of
values:
- We can likewise create methods that accept parameters in Alice,
but now the parameters can be Objects
in our scene.
- Example: The Funky
Chickens Alice world. We will demo this and discuss how the
parameters are passed into the world.doADance method. Change a
chicken head, for example, and see what happens.
- Note: We use the distance behind
method and the as seen by other
chicken measuring system to move the chicken back after it has
danced. (distance will be negative, relative to the other
chicken)
- Here is a helicopter example (heli.a2w).
When we first run the example, the helicopter does not move as we
expect. Change the move up
method so that as seen by
refers to the ground object and play the scene again.
- Class Project: Create a
new world method in the Funky Chickens World that will take a chicken
parameter and carry out some actions with that chicken.
- Review how to create a parameter. Note how parameters can be
Numbers, Boolean, Objects, or other types (such as Strings).
- Difference between Parameter
and Variable:
- Parameter is an
outside Object that gets passed into the method (exists outside method)
- Variable is a value
only used inside a method
(doesn't exist outside method).
- Test out your new method.
- Event Handling
- So far our Alice worlds have been fun to watch, but there has
been no user interaction, we are just passive observers of what is
happening on the screen. To add user interaction to our Alice
worlds, we will need to have a way for Alice to understand when a user
moves a mouse or types on the keyboard. These actions are called events.
- An event
is something that happens due to user activity and to which the
computer responds.
- Event Handling: How do
we link events of the user to program action? We handle an event as follows:
- 1) The event must be captured by the program, i.e. must be
linked to some method.
- 2) Once captured, the method should carry out some kind of
response to the event.
- Example: Suppose we
want to create a flight simulator program. We need a way to control
movement of a plane. In the world biplane.a2w we create
event handlers for keyboard control of a biplane's movement.
- Note how events are created and then linked to a method.
- Note that the starting of the animation of an Alice world is itself an event, which is handled
by the method world.my
first method.
- Note how we set the camera to track the plane's movements.
Another option would be to set the camera's point of view to the
biplane.
- camera -> set point of view
to biplane
- Try this - camera doesn't continuously set point of view,
need to embed this in a while
statement.
- Review and Demo all possible events