Parameters and Event Handling Methods

January 5, 2015

Today’s hot topics: parameters, event handling methods, incremental development, control of flow

Objective: Apply concepts-passing parameters to event handling methods. Write a program that passes parameters to event handling methods.

As you know, parameters are powerful tools. They allow us to customize methods to work with different objects and different numeric values. They are useful in building either world-level or object-level methods.

Example…

1. Scenario: The stage manager for an ice show production is testing various lighting effects before the ice skaters perform. Three lights (Lights) have been positioned around a white cube (Shapes) set on the ice surface (FrozenLake, in Environments). The lights are carefully positioned so the red light is reflected on one side of the cube, the blue light on an adjacent side, and the green light in the middle. To create a dark sky, I will use the space template. To test the lighting effects, the brightness of the world’s light is dimmed. This example is to create an animation that allows the user to click on a light to turn it off. Each time a light is clicked, that light is turned off. When all three lights are turned off, the world scene will be totally dark.

2. Storyboard design-parameters and event handling methods

To design an interactive storyboard, we must give some thought to what events will occur and what event handler methods are needed. I will allow the user to select the light to be turned off. In order to turn off a light in Alice, it is necessary to do two things: the light itself needs to be blackened, and it needs to stop emitting light (which is accomplished by blackening its emissive color).

3. Implement.

·  build scene

·  create event handling method

·  link events to event handling method

4. Test-When parameters are used in event-driven programming, it is especially important to run the animation several times, each time causing different events to occur to be sure that each possible parameter value works as expected.

A not so simple example…

In an event-driven program, the response to an event may involve multiple actions. Writing an event handling method to carry out the response can become a bit messy. One way to deal with a complex response is to use stepwise refinement to break down the event handling method into smaller pieces.

1. Scenario-An ancient Greek tragedy. In Greek mythology, Zeus was an all-powerful god. If Zeus was angered, a thunderbolt would be shot out of the heavens and strike anyone who got in the way. The initial scene is constructed with Zeus (People) overlooking a temple scene (Environments) from his position on a cloud, a thunderbolt object, and some Greek philosophers named Eruipides, Plato, Socrates, and Homer. The initial temple scene can be found in Mrs. Fox’s outbox in the CompSci folder. Go to Mrs. Fox’s outbox (sfox) to see her program. The thunderbolt object has been hidden within a cloud (the one immediately in front of Zeus). Also, a smoke object (a special effect) has been positioned 5 meters below the ground. The smoke is not visible in the initial scene.

2. Zeus Design Storyboard. To make this animation interactive, we will let the user choose the object that will be the next victim of Zeus’s anger. When the user clicks on an object, the object will be passed to the event handling method. What actions need to occur in response to the mouse-click event?

First, Zeus will turn to face the selected object and the thunderbolt will be made visible.

Then, the thunderbolt will flash down to strike the object.

Smoke will be used as a special effect to make the object appear to meet a sad fate.

Then, the lightning bolt must be repositioned to prepare for another lightning strike.

3. Activity-Recreate Zeus and add this modification: In this world, a philosopher has been zapped by lightning and is scorched to show he has met a tragic ending. The philosopher is still in the scene, however, and the user can zap him again. Revise the program to make the zapped philosopher fall down below the ground, where he can’t be mouse-clicked again.