Events Tutorial

Henry Qin
Susan Rodger
Duke University Department of Computer Science
Summer 2008

0. What are events for?

In everyday life as well as in video games, events happen and we need to respond to them. If a car got off the road in a racing game, it should no longer be able to maintain its speed. It would be nice if we could provide instructions to a user and allow him to dismiss them with a click of his mouse when HE is done reading. At the time that a game begins, certain things must happen. All all these are "events" in Alice; they allow the Alice programmer to respond to something happening in the Alice world. In today's tutorial, we will learn to make our programs respond to mouse clicks and keyboard presses, as well as handing camera control over to the user.

1. Loading the World

Download the file that we'll be working with today here. Save it in a directory that you can find again, and then start Alice and open it in Alice. NB: 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.

This program will already have some code written to make the characters do certain things. You will be using it, but you don't need to understand how it works, although it's great if you do.
I want to note that, in this tutorial, I'll use the word "action" for Alice's responses to events, although Alice and programming languages call them "methods." I think it is more intuitive to think of "taking an action" in response to an event than calling or "invoking a method."

2. First Event: When the world starts.

When you first start any Alice world, the event "When the world starts, do World.myfirstmethod" is always there by default:

The "event" part of this statement is "When the world starts"; Alice's response to this event is " first method." Try running this world and observe that nothing happens when the world starts. This is because the response, " first method," does NOTHING.

Let's make something happen when the world starts. Let's make the blue bird move to the other bird when the world starts. Click on the bluebird in the object tree in the left pane:

Now, scroll down the methods of the bluebird until you see "Bluebird move to." Click and drag this to replace " first method" in the Events pane. When you drop it, a drop down menu will appear, and you sould select bird1->the entire bird1:

Now Play your world and observe what happens as the world starts. You might get a warning when you try to play, but click okay and continue. Notice that now the blue bird will move to the other bird when the world starts. Delete the event "when the world starts" by right-clicking on it and selecting delete. Play your world and observe that, once again, nothing happens when the world starts. For the remainder of the tutorial, we do not need anything to happen when the world starts. (If you don't want to see the annoying warning message every time you run your world, you can delete " first method" for good measure. Just right click and hit delete.)

3. User-Generated Event: Keyboard press.

Now, we are going to see how to make Alice respond to a key pressed by the user. In the events pane, click "create new event" and select "when a key is typed."

You should see now that the only event says "When any key is typed, do Nothing.

We can replace "do Nothing" with any action we want Alice to take, including the one in part 2 of this tutorial. Just to show that the action does not matter, let's replace do "Nothing" with bird1's FlyForward method. Just as we did earlier for the bluebird, click on Bird 1 in the object tree and drag the FlyForward method to replace "Nothing."

Now, when you Play your world, pressing (almost) any key should cause the first bird to fly forward. In many games we'd like to be able to differentiate between the different keys pressed, and produce a different behavior when a different key is pressed. This is relatively simple in Alice - click on "any key" and choose a key. I've chosen the F key, since we're making the bird fly forward.

Now, try to follow the steps above to make the bird1 fly backwards when the B key is pressed. When you are done, your screen should look like this:

Play your game to verify that the bird will fly backwards and forwards on demand. Note also that the bird will generally not prematurely end an action to start a new one. Instead, it will tend to finish the action it started before responding to the event of the new keypress.

4. Mouse Click

The Mouse Click event can be triggered either by any mouse click, or by a mouse click on a particular object. The procedure for creating this event is very similar to the keyboard press. Begin by clicking on "create new event" and selecting "when the mouse is clicked on something"

As with the keyboard event, the event should read "When (mouse image> is clicked on anything, do Nothing.":

Now, we want the blue bird to approach when we click on the green button in the button panel. The easy way to tell Alice that it's the green button is to go to the object tree and open the ButtonPanel, and then drag greenButton over "anything."

Since we want the blue bird to approach, we click on the blue bird in the object panel and drag its "approach" action over the "Nothing" in our event.

Now, when we play the world, we should observe the blue bird approaching us when we hit the green button. There is no way to stop the bird from crashing into us at that rate, so we would like some event to stop the blue bird. Since red usually means stop in traffic lights, try to use the procedure above to make the blue bird stop when we click the red button. When you are done, it should look like this:

Finally, just to make it absolutely clear that actions can be ANYTHING, we will make the last, purple button cause the other two buttons to appear or vanish. Create a new mouse event. Then, replace "anything" with actualbutton, which is a part of "button." See below:

Now, which action do we use to make the button panel invisible? Well, if you click on "ButtonPanel" in the object tree you'll see that there's an action (method) called "toggleVis." Drag this over the "Nothing" in the events pane.

Play your world, and verify that all the events are triggered as expected.

5. Misc Events

If you examine the drop-down menu for creating an event, you'll notice that the last two events allow the mouse and/or keyboard to move the camera.

I encourage you to try these, one at a time. For the keyboard, use the arrow keys. For the mouse, you have to click and hold the mouse to activate the event.
There are also two events above those, which enable the user to control any object freely with the keyboard and mouse. Feel free to experiment with the keyboard one, but don't try the mouse until you've learned what a List is in Alice. You should, however, understand the issue with giving the user this much freedom to travel your Alice world. They could get lost, miss the animation you meant for them to see, or see parts of your world you did not intend them to see until a later point.

Professor Rodger's Homepage

Author's Website

Main Tutorial Website

Alice Materials at Duke