Exercise 8 — Show the Results of the Dice
Applet |
---|
This exercise also introduces the concept of containment. Because the logic to generate the randomly generated dice roll involves some wiring, creating a container to hold this logic makes the display of the title in Wire view easier to read. Containment is a way to organize modules. As you will see in following exercises, it can also be a technique for making Hyperwire code reusable.
A message appears asking about the style in which the container will be displayed. Answer No. User Module containers are for functional groups such as the one in this exercise.
Hyperwire places the container in your title.
The container labeled Show Result will do exactly what its label says. From the point of view of the Dice title, this is all it needs to know about what the container's logic does. It does not need to know how the container shows a result. This is part of the advantage of using containers.
Assuming that the container will show a result, you can wire the container before you finish defining it.
The container will display a result once the dice stop rolling, and the display will turn off when the user clicks the button again. Having set this up, you can define the contents of the container.
Hyperwire opens a new window that shows only the contents of the container. While you work in a container, you don't have to worry about logic outside the container.
The sprites in the Show Results container don't move, so accept the default path and don't worry about editing it.
Like the tumbling die animation, Diefaces.gif has multiple subimages, but in this case you want it to display only one face at a time. The reason for using a Sprite module is that a sprite can display a single numbered frame.
Frame 1 shows the die face with one spot, Frame 2 shows the face with two spots, and so on.
This prevents the sprite from animating or moving about the screen.
The two dice are now ready to display numbers.
The input and output port buttons below the Wire view of a container's window are how a container reports "outside" events to the modules within it.
An isolated output port appears in the Wire view. This port will be activated when in the outer, parent module, Tumbling Die 2, sends its On message to the Show Result container.
To wire from the On port, click the wire button on the lower part of the On port's icon.
Each of the wires you just created has an unsatisfied parameter icon that looks like a "not" symbol. As in exercise 6, these icons indicate that each wire has a parameter you need to fill in. In this case, the parameter is the frame index number telling the sprites which frame to display. You will use a Random Number module to simulate randomly rolling dice.
The name of the missing parameter, Frame Number, pops up. Click the name. A wire appears. Drag the wire to the Random Number module and connect it to the input port, Get Random Number Range.
As you can see, each of the new wires has two parameters to set. These parameters are the upper and lower limits of the range of random numbers the Random Number module will return. These limits correspond to the dice you are simulating. The values must range from one to six.
The Wire Parameter editor appears.
Note: Click the wire itself, not the circles that indicate parameters. Clicking a parameter circle causes Hyperwire to add a draggable wire. This is not the way to set these particular parameters.
You will set the Min and Max range limits to be constants.
When you run the title, after the dice roll across the window, two dice appear, showing a randomly generated result.
Run the new title to test it, and then save your work.
This is the end of exercise 8. Exercise 9 shows how to make one die a reusable module.