Well, we've seen a few different kinds of events now. We've seen keyboard events, and mouse events, and the "world starts" event. In the animation of the objects dropping off the building, we created three separate event handlers, one for each object. While this worked fine, it seems somehow wasteful - rather like writing three bits of code that are all doing the same thing.
Alice provides a way of simplifying such event handlers. Let's explore with a simple car game.
In a game, there are often many things happening at once - different actors all doing their own thing. We're going to write a car game with two different activities going on:
A car will be in motion. The car will simply move forward for about two minutes - all the time the game is running.
The user will be able to click on particular points on the screen - these are magnets - and when one is clicked on with the mouse, the car's direction is changed to point at the magnet.
The point of the game is to try to use the magnets to steer the car along the roads! Silly, yes, but fun.
To get going, open up the world Car Game.a2w, located at the bottom of the page, and in Alice.
Download the world at the bottom of the screen: Mod6.6CarGame.a2w
In this world, we have changed the camera view to be way, way up in the sky looking straight down on a city terrain laid out within a square shape, with green fields all around - the black lines are city streets. Notice that in the middle of each side of the city square, there is a magnet! And in the top left corner of the city there is our friend, the red sports car.
Let's now implement each part of the game. First let's get the car moving. In my first method, add a single line of code that gets the convertibleCorvette to move forward 2000 meters over a duration of 120 seconds. (When you enter these large numbers, use the physical keyboard, not the keyboard that comes up on screen - it seems to work better!) The code should look as follows:
For the second part, we need an event handler - but before that, we need a method to process the event -- that is a set of instructions we want to have happen when the event occurs. The method is going to make the car move towards a magnet. The method will take a single parameter, one of the magnets, and the code in the method will direct the car towards that magnet.
So, create a new method, calling it feel the force, and create a single parameter in the method - called, say, magnet - and make sure the parameter is of Object type. Now add a single line of code into the method - getting the convertibleCorvette to turn to face the magnet parameter. You should have a method looking like:
Now to connect up clicks on the magnets by the user through to the feel the force method. If we did just as in the previous page 6.5, then we'd create four different event handlers, one for each magnet. But this time we're going to use just one event handler....
So - create a new event - selecting the When mouse is clicked on something option. You should have an event looking like the one below:
This time, leave the "anything" box as it is. Click on the "Nothing" and select feel the force and then the expressions option, and then the single option object under mouse cursor.
Finally, you should have an event handler looking like this:
Reading it in English, it says that when the mouse is clicked on something, call the feel the force method passing as a parameter whatever it was that the mouse was clicked on.
Now run it and see what happens when you click the mouse on the magnets. Note that the car moves slowly to start with, but seems to speed up. Try to keep the car on the roads!!
[Q6.6.1]: This has certainly reduced the number of event handlers compared to the previous example in section 6.5. Given the specification of the game above, can you detect any odd behaviors resulting from using this one method?
[Q6.6.2]: The odd behavior is that if you click on the city terrain, the car moves directly towards the centre of the city. Why do you think that happens?
A mouse click on an object other than a magnet is treated just the same - the object clicked on is passed to feel the force, where the car is made to point at that object. In the case of a click on the city terrain, the car is therefore made to point to the center of the city terrain.
Don't forget: Download the file linked below. Launch Alice on your computer, then use File -> Open World and select the file you downloaded.
Save the world as LastNameFirstName-6.6CarGame.a2w