Appearances, World Variables, and Timing
Josh Burker
5/20/2003

When I first set out to take apart Cameron Hayne's DoubleClick World I had no idea of the complexity behind this World. However, Mr. Hayne accomplishes his goals of this World (variable-speed double-clicking practice with audio and visual feedback for the user) using very little code, and it runs beautifully.
For the version of DoubleClick that you can use on the Cocoa Projects page, I set the speed of the World to slow before I Built an AutoPlayer. This seemed to give the best consistency for use as an imbedded plugin or as an AutoPlayer; fast seemed to run too fast, so the user could not get in the second click before the program determined she or he had not clicked fast enough.
This page will lead you through the code in DoubleClick and show you how the program registers that the user has clicked; waits for the second click and provides audio feedback if the user completes the double-click sequence; and if it does not receive a second click, resets itself for the next attempt at double-clicking.
I am assuming that you have the basic understanding of how to create a Click-Response Rule for an object.
I will introduce the Cocoa programmer to the following skills:
Throughout this page, when appropriate, you can click on the different pictures I use to illustrate the text to get larger versions of them, which will allow you to see the code for yourself.
Using Appearances in Cocoa
This World depends in part
upon the use of Appearances to register that the user has
double-clicked on the Practice Area. Clicking on the
Practice Area changes the appearance of the
Practice Area from ready to waiting
(see figures 1 and 2). When the user clicks on
the waiting Appearance of Practice Area, the
program registers this as a "double-click." If the second
click happens within the time limit imposed by the
"Double-Click Speed" setting, then the user receives audio
feedback to confirm that she or he
double-clicked. The Practice Area is
a Piece with a Click Response assigned to it. When the user
clicks on the Practice Area there is a rule that
changes the Appearance of the Practice Area from
ready to waiting. Here's a look at the code
that causes this Appearance change (see figure
3): As you can see, the
Rule changes the Appearance of the Practice
Area from ready to waiting.
However, there are also a couple of other Actions
that occur during this Rule that make this World
function. First, the Rule
tells the program to take the "dbl click
delay" World Variable and put it in the
delay piece variable. We'll look at the uses
for variables in the next section. Second, if the user
clicks on the waiting Appearance within the
time allowed (this is determined by user-defined
"Double-Click Speed" settings), the program will
offer audio feedback that the user has
"Double-Clicked" by playing the Sound "dbl
click." figure 2


Variables for Cocoa Pieces
Bringing it All Together
Now we'll examine how these Rules work together to make this World work.
When the User clicks on the waiting appearance of the Practice Area, the Rule not only changes the Appearance of the Practice Area but it also puts the number in the World Variable "dbl click delay" into the Piece variable "delay." Next, the program runs the Rule that starts this "delay" variable counting down (see figure 6):
As you can see from this
Rule, every time the Rule runs the program subtracts 1 from
the Piece variable "delay." There is an additional Rule
("count
down rule") to check
whether this "delay" Variable has reached 0. The "time's up"
Rule then resets the Practice Area Appearance to
ready to reset the board so the Practice Area is
ready to be clicked on again. If the user clicks within
the time allowed, Cocoa runs the Rule that plays the "dbl
click" Sound: As you can see, if
the user clicks on the Practice Area within the
time allowed by the "Double Click Speed" setting,
Cocoa will change the Appearance of the Practice
Are to ready (so the user can try again), as
well as play the Sound "dbl click."

Conclusion
As you can see, Cocoa's Rules can be written in such a way as to allow for Actions that one might not think possible in Cocoa (for example, looking for and recognizing a double-click). As you'll see when you start playing with Cocoa, there are many ways to do the same thing. This particular World, with its well-written Rules and use of World Variables, qualifies as an Advanced Example of programming in Cocoa.