So far, we've only made use of key press events - that is, events created as a result of the user pressing a key on the keyboard.
Alice also lets us write programs that react to the what the user does with the mouse.
We're going to work with the world we saw back in module 4 where objects fell off buildings. We're going to change the behavior of the program to include some user input using the mouse.
So - to get started, open up the world FallingObjects.a2w which you can download at the bottom of this page.
Download the world at the bottom of the screen: Mod6.5FallingObjects.a2w
Run it again if you need to be reminded of what it does. There's certainly no interaction with the user!
Get the code for My First Method up - and delete the three calls to the method fall. We don't want the objects to fall off the building in the same order every time -- so we remove the three instructions that caused that to happen.
Calls to delete:
[Q6.5.1]: If you were to hit Play now, what would happen? How do you explain that?
Now create a new event handler - selecting "When the mouse is clicked on something" from the options. You should get something like this:
Click on the red "anything" text and select one of the three objects. Then click on the red "Nothing" text and select the fall method, and, as a parameter, the same object as you put into the "anything" text. Take a look at what you've created. An example, using the blender, would look as follows:
[Q6.5.2]: What do you think is NOW going to happen when you run the code?
Try it out now, to confirm your prediction.
Clicking on the object you selected for the event handler should result in that object, and that object alone, falling to the ground.
Now create similar event handlers for the other two objects on top of the building. Run it and click on the objects.
Of course, you don't have to always act on the same object you clicked on. For example, you could add an event handler that catches a mouse click on the building itself, and causes the blender to fall off the roof. Try it out now, to convince yourself that it would be ok!
[Q6.5.3]: Finally, what do you think will happen if you click on an object for a second time, once it has fallen to the ground?
Try it out, to confirm your answer.
Did the result surprise you? Remember that the laws that apply in an Alice world don't necessarily match the laws of Physics that we are all working with in real life!! Your skill as a programmer is (most of the time) to make the virtual world you are creating act as much like the real physical world as possible.
It should have 3 new events.
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.5FallingObjects.a2w