MCS-170 The Nature of Computer Science
Alice Lecture 4
1. Parameters and Event Handling
- Indefinite Loops
- Event Handling
- Indefinite Loops - The While
- 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
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)
EskimoGirl and snake (version 2)
[ Girl is movable through the use of an Event - more on Events later]
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
- 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
- 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
- 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
- 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
- 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
- camera -> set point of view
- Try this - camera doesn't continuously set point of view,
need to embed this in a while
- Review and Demo all possible events