Duke University Department of Computer Science
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.
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."
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 "world.my first method." Try running this world and observe that nothing happens when the world starts. This is because the response, "world.my 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 "world.my 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 "World.my first method" for good measure. Just right click and hit delete.)
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.
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
Main Tutorial Website
Alice Materials at Duke