Concept
This simple hide-and-seek game challenges users to find a hidden bug (literally speaking, that is) within 25 seconds. The bug hides under one of 48 squares. Mousing down on a square makes it disappear; releasing the mouse button puts it back. If the user chooses the square in which the bug is hiding, s/he wins. The bug moves randomly to a new position every 2 seconds. If the user doesn't find the bug in 25 seconds, the game defaults to a losing ending.
Architecture
The project has five layers, two of which contain the single frames for the winning and losing outcomes. The remaining three layers contain the covering squares (all 48 in the same layer), the lurking bug, and the timer display.
The covering squares are all copies of a single movie clip with two handlers, one for mouseDown that changes the visible property to false, the other for mouseUp that changes visible back to true. These handlers use the localization code introduced a few weeks back in order to determine to which square the click belongs.
The bug is also a movie clip, this time with three handlers in its script; which brings us to the important bits.
Time control scripts
In the bug movie, a localized mouseDown handler branches the main timeline to the winner's frame. An enterFrame handler changes the bug's position every two seconds. This handler uses getTimer() to delay the moves. The getTimer() method returns the number of milliseconds (thousandths of a second) that have elapsed since the movie was started. This value can be very useful:
onClipEvent (enterFrame) {
if (getTimer() > moveTime) {
moveTime = getTimer()+2000;
this._x = random(600);
this._y = random(200);
}
}
This handler refers to a variable called moveTime which is defined at the start of the movie (see below). When a frame containing the bug movie is entered--which is every frame during the 25-second duration of the game, since the playback head loops in and out of frame 1 until there is an outcome--the current time reading, getTimer() is compared to moveTime. If the former exceeds the latter, then the X and Y coordinates the the bug movie are given random values, and moveTime is reset to the current time + 2000 milliseconds.
You may be wondering how moveTime gets its initial value. This takes place in a third handler that executes only once, when the main movie is started:
onClipEvent (load) {
moveTime = getTimer()+2000;
}
The final element in the game is the countdown display, contained within a movie clip called timer. This clip has one simple handler:
onClipEvent(enterFrame){
this.timeReadout = (25000 - getTimer())/1000;
if(getTimer() > 25000) _root.gotoAndPlay(10) ;
}
The variable this.timeReadout corresponds to a dynamic text element that shows the result of some simple math in which the current value of getTimer() is subtracted from 25000 (i.e., 25 seconds in milliseconds); this value is then divided by 1000 to yield a value in seconds. The if statement handles the consequences of a timeout.
Restarting the game
In case the player wants to experience more self-inflicted frustration, both the winning and losing outcomes include a button that restarts the game. Initially this button was scripted simply to move the playback head back to frame 1; in that case, however, the value of getTimer() does not return to zero, since the movie is still playing. If the first outcome was a timeout, the player automatically loses the second round. If s/he still has some time on the clock, it is quickly used up.
You might think that reloading the present movie, using either loadMovie() or getURL(), might do the trick. Unfortunately, neither method works--yet another quirk in the design of ActionScript.
The timer will reset only if another Flash movie or another HTML page is loaded into memory. This might be done in either of two ways: using loadMovie, by loading a movie with a script that reloads bughunt.swf, or using getURL, by loading a Web page that contains an automatic HTTP-refresh that reloads bughunt's hosting page. Our demo uses the latter method, which is why you'll find a second page called bughuntReload.htm in the file set.
Note
Thanks to Emily Carr for calling our attention to getTimer().
