In this section you'll add a timing device to the game, and also be introduced to another Lingo timing mechanismthe timer. The timer has been around for as long as Director and uses what's known as ticks as its unit of time. (There are 60 ticks in a second.) Ticks aren't as precise as milliseconds, but for counting seconds in a game, they will do nicely.
Let's begin by adding a text field to display the timer in the game.
Select frame 1 of channel 2, in the Score by clicking it. Select the Text tool in the tool palette and drag out a text field in the upper-right corner of the Stage:
Enter the text Time: 00.00 in the field. Click away from the field to deselect it. Adjust the sprite's span so that it begins at frame 1 and stops at frame 10.
You'll use Lingo to replace the text when the game is running, but entering it into the field allows you to see the formatting and provides visual reference during authoring.
Double-click the new text cast member to open a Text window. Press Ctrl/Command+A to select the text. Change the font to Arial, and click the Align Right button and the Bold button. Finally, use the tool palette to change the foreground color to white and the background color to black.
Enter the name timeDisplay for the member, and close the text window.
With the text sprite in place, you need to give it a behavior that will display the time as the game progresses.
Right-click the text sprite and select Script from the context menu. Delete the default mouseUp handler and add the following:
property myCount on beginSprite me the floatPrecision = 2 _movie.startTimer() myCount = 0 end on enterFrame me if myCount = 10 then currTime = _movie.timer secTime = currTime / 60.0 sprite(me.spriteNum).member.text = "Time:" && string(secTime) myCount = 0 else myCount = myCount + 1 end if end
First, the myCount property is defined. This will be used as a way to update the on-screen timer display every ten frames. Setting the text of a cast member isn't the speediest thing in the world, so you don't want to do it on every frame if you can help it. Doing it every ten frames won't be noticeable in the display, but the rotations of the cards will be smoother.
Within beginSprite, the first line of Lingo sets the floatPrecision to 2 (by default it is 4). The movie property floatPrecision affects how floating-point numbers are displayed. It doesn't at all affect how they are calculated, however; calculations are processed in full precision regardless of this setting. What the setting really does is affect how many decimal places in a floating-point number are displayed. For example, look at the following taken from the Message window:
the floatPrecision = 4 trace (22 / 7.0) -- 3.1429 the floatPrecision = 2 trace(22 / 7.0) -- 3.14
As you can see, by setting it to 2, only two decimal places are displayed.
The next line of code calls the movie's startTimer method, which initializes the timer and sets it to zero ticks. Finally, myCount is initialized to 0.
Within the enterFrame handler, the first line of code checks to see if myCount has reached 10. When it has, the code first gets the current value of the timer and places it into the currTime variable. It is then divided by 60.0, because there are 60 ticks in a second, to get the number of elapsed seconds. The text of the member is then set to the time string and myCount is reset back to 0.
When myCount is not yet 10 the code within the else gets executed and myCount simply gets incremented by 1.
Rewind and play the movie.
As you play, the timer in the upper-right corner of the Stage updates every tenth frame, which will feel like a constant update. You should experiment with commenting out the code that causes the display to update every ten frames so that it updates on every frame instead. On some machines the effect will be negligible, but on others you will notice some slowdown.
Right now, when you find all the matches, the game just sits there and the timer keeps on going. So you need to add a method of knowing when all of the matches are found.
Stop the movie and save it before continuing.