The example below shows an educational applet illustrating a classic problem of discrete probability, the Cereal Box Problem. For this problem we imagine a cereal company that randomly distributes five prizes, one into each of its cereal boxes. The question is to find the expected number of boxes that must be purchased in order to collect all five prizes. (The prizes are denoted by the big red numbers on the boxes.)
For this application there is quite a bit of setup on the stage, so we provide the file CerealBoxStart.fla with this part already done. In this file, the text and buttons (including the little "cash register" button) are already created and placed on the stage. The cereal box movieclip will be our focus -- the instructions for creating that are included below.
Creating the CerealBox movieclip
We would like to be able to generate any number of cereal boxes with our script, and we would like each cereal box to have a number placed on it. We will do this by converting a "cereal box" graphic to a movieclip symbol, adding a textbox to its timeline, and linking the movieclip properly in the library so that it can be used by ActionScript.
Step 1. Create a graphic that looks like a cereal box (Download the file cereal.bmp if you want to use the one drawn by my daughter Jess.) and import it to the Stage using File > Import > Import to Stage (or Ctrl+R). Select the graphic and Modify > Convert to Symbol, and make it a MovieClip named CerealBox with its registration point in the center. (The name and registration point are only important if you want to use our final code exactly as is.)
Step 2. Double-click on your CerealBox movieclip and a new timeline will open up specific to this movieclip. Within this timeline, add a dynamic textbox with instance name txtPrize to the bottom right corner. (I used a bold 60pt font.) Click the phrase "Scene 1" at the top of panel showing the stage to return to the main timeline. (See the screen shot below.)

Step 3. Open the Library panel (Ctrl+L) and right-click on the CerealBox movieclip. Choose Linkage from the resulting menu, and select Export for ActionScript as shown in the screen shot below.

Now we will be able to refer to CerealBox as a class in ActionScript, so we will be able to create instances of it at run-time. Moreover, we will be able to place text in an instance <instancename> of the CerealBox class by referencing the textbox as <instancename>.txtPrize. We can similarly reference any objects that we placed in the CerealBox clip's timeline, including buttons or other movieclip objects.
In the interest of space, we discuss the code for this applet on the next page.










