| Pentium 100-MHz PC (or faster) |
| at least 16 MB of RAM |
| Microsoft Windows 95, 98, or NT 4.0 (x86) |
| Microsoft Visual Basic 5.0 or higher |
| Microsoft Agent core components |
| Peedy character file |
Create a new Standard EXE project in Microsoft Visual Basic®. Add the Agent control
to your new project by selecting Components from the Project menu, and checking the box
next to Microsoft Agent Control 2.0. Insert the Agent control on Form1 of your application.
Add this line to the declarations section of code for Form1:
Dim Peedy as IAgentCtlCharacter
|
Add the following lines to the code for Form1:
Private Sub Form_Load()
Agent1.Characters.Load "Peedy", _
"peedy.acs"
Set Peedy = _
Agent1.Characters.Character("Peedy")
End Sub
Private Sub Form_Unload(Cancel As Integer)
Agent1.Characters.Unload "Peedy"
End Sub
|
If you try to start the application now, nothing will happen. Why? Although the first
line of the Load event loaded Peedy into memory, your application never told the
Agent control to show him. To solve this, insert two command buttons on Form1. Name
the first one cmdShow and the second cmdHide. Change their captions to Show and Hide.
Then, add the following lines of code to Form1:
Private Sub cmdShow_Click()
Peedy.Show
End Sub
Private Sub cmdHide_Click()
Peedy.Hide
End Sub
|
Now start your application and click the Show button. Peedy should fly to the corner of your screen and sit there, until you press the Hide button or close the application.
You will notice that Peedy does not always sit still while he is
on the screen. This is because Agent characters have idle animations,
which occur when there are no actions in the message queue of the
character. If you leave Peedy alone for a few minutes, you can watch
his some of his idle animations.
Now that you can make Peedy show and hide in your application, the next step
is to have him interact with the user. One easy way is through the
Speak method, which displays a word balloon containing
a message, similar to a comic strip. If you have a text-to-speech engine
installed, your Agent character will even be able to speak.
To continue with this example, you will want to download the Lernout &
Hauspie TruVoice Text-To-Speech Engine (1mb) from the Microsoft web site. This
example will run without it, but the characters will not have audio.
Continue with the application from the previous section, and add a text box
to Form1. Name this text box
txtSpeech. Also, add another command button to the form,
and name it cmdSpeak. Set the caption of the command
button to Speak, and delete the text inside the text box.
Next, add the following event handler for the Click event
of cmdSpeak:
Private Sub cmdSpeak_Click()
Peedy.Speak txtSpeech
End Sub
|
Now start your application and click the Show button. Then enter something
into the text box, and click the Speak button. Peedy will read the text in
the txtSpeech box.
The top-left corner of the screen is not always the ideal position for Peedy.
This part of the screen often contains menus and toolbars, where Agent
characters can get in the way. As you may have already noticed, the user can
drag Agent characters on the screen, but characters also have a method called
MoveTo, which allows developers to control the position
of the characters.
MoveTo requires two parameters: the x and y screen
coordinates of the new position. These are measured in pixels from the top-left
corner of the character. If MoveTo is called before
Show, the character will appear at the new position.
Otherwise, the character will start at its original position, and move to the new
position. In the case of Peedy, a flying animation will be shown.
Add two new command buttons to the example from the previous section, and name
them cmdPosition1 and cmdPosition2.
The Position1 button will move Peedy to the top-left corner of the screen, and the
Position2 button will move him to the bottom-right corner.
Add these lines for the Click event of cmdPosition1:
Private Sub cmdPosition1_Click()
'Move Peedy to the top-left corner of
'the screen
Peedy.MoveTo 0, 0
End Sub
|
The code for the first position was simple, but the second will be much more
complicated. First, we will have to determine the resolution of the user's
screen, by using the Screen.Width and
Screen.Height properties. Since
Screen.Width and Screen.Height
return measurements in twips, we then must convert these values into pixels.
Finally, we must account for the width and height of Peedy, since the MoveTo
method uses the coordinates of the top-left of the character.
This is the code for cmdPosition2, which will move Peedy to the bottom-right corner of the screen:
Private Sub cmdPosition2_Click()
Dim nScreenWidth, nScreenHeight As Integer
Dim nPeedyWidth, nPeedyHeight As Integer
'Get screen width and height (in pixels)
nScreenWidth = Screen.Width / _
Screen.TwipsPerPixelX
nScreenHeight = Screen.Height / _
Screen.TwipsPerPixelY
'Get Peedy's width and height (in pixels)
nPeedyWidth = Peedy.Width
nPeedyHeight = Peedy.Height
'Move Peedy to bottom-right corner of
'screen
Peedy.MoveTo nScreenWidth - _
nPeedyWidth, nScreenHeight – _
nPeedyHeight
End Sub
|
Now start your application, and show Peedy. When you click the Position2 button,
he will fly to the bottom-right corner of your screen. Press Position1, and he
will fly back to his original position. Also, notice that if you attempt to move
Peedy while he is hidden, he will appear in the new position the next time he is
shown.
There is one important method in Agent characters that we have not covered: the
Play method, which plays one of the animation sequences of
the character. It takes one parameter, a string containing the name of
the animation to play. A complete list of animations for each character is available
in the Microsoft Agent documentation from the Microsoft web site, but a shortened
version is shown below in Table 2.
| Announce | exit branches | yes | Paper airplane flies in and unfolds |
| Congratulate | exit branches | yes | Displays blue ribbons |
| DoMagic1 | none | yes | Raises magic wand |
| GetAttention | GetAttentionReturn | yes | Jumps up with wings outstretched |
| Greet | exit branches | yes | Bows |
| Pleased | exit branches | yes | Smiles |
| Process | none | no | Uses calculator |
| Read | ReadReturn | yes | Opens magazine, reads, and looks up |
| RestPose | none | yes | Neutral position |
| Search | none | no | Rotates with telescope |
| Suggest | exit branches | yes | Displays light bulb |
| Think | exit branches | yes | Looks up with wing on face |
| Wave | exit branches | yes | Waves |
| Write | WriteReturn | yes | Takes out pencil and pad, writes, and looks up |
Add a command button named cmdProcess to the form of the
example from the previous section. Change the caption of the button to Process.
For the Click event of cmdProcess,
add the following lines:
Private Sub cmdProcess_Click()
Peedy.Play "Process"
End Sub
|
Start your application, show Peedy, then press the Process button. Peedy will use a
calculator, and then return to his normal position.
Create another button, named cmdWave, the same as the step above, except change the
code for the Click event to:
Private Sub cmdWave_Click()
Peedy.Play "Wave"
End Sub
|
When you start your application and press the Wave button, Peedy will wave, but he will remain frozen in the
last frame of the animation sequence for a few seconds. How can you prevent this?
The problem is simple. Certain animations do not return the character to their neutral
position until another animation is played. They use return animations called exit
branches. Since the animations are queued, Peedy has reached the end of his queue, so
he freezes at the last frame. Once no new animations enter the queue for a few seconds, he
automatically enters his idle sequences, and returns to his normal position.
To prevent this, change the code for the Click event of cmdWave to the following:
Private Sub cmdWave_Click()
Peedy.Play "Wave"
Peedy.Play "RestPose"
End Sub
|
Now when you start the application and click the Wave
button, Peedy will wave, and then return to his normal position.
Some animations, such as GetAttention and
Read, require special return animations. Simply call
the Play method with the name of the return animation
immediately after calling the original animation.
Another feature of the Play method is the ability
for a character to speak during an animation sequence. Not all animations
support this feature (see table 2).
Place one more push button on the application, and name it cmdGreet. Add the
following code to handle the Click event of
cmdGreet:
Private Sub cmdGreet_Click()
Peedy.Play "Greet"
Peedy.Speak "Hello, I am Peedy."
Peedy.Play "RestPose"
End Sub
|
Start the application, show Peedy, and press the Greet button. Peedy will bow and say hello.
Dim Peedy As IAgentCtlCharacter
Private Sub cmdGreet_Click()
Peedy.Play "Greet"
Peedy.Speak "Hello, I am Peedy."
Peedy.Play "RestPose"
End Sub
Private Sub cmdHide_Click()
Peedy.Hide
End Sub
Private Sub cmdPosition1_Click()
'Move Peedy to the top-left corner of
'the screen
Peedy.MoveTo 0, 0
End Sub
Private Sub cmdPosition2_Click()
Dim nScreenWidth, nScreenHeight As Integer
Dim nPeedyWidth, nPeedyHeight As Integer
'Get screen width and height (in pixels)
nScreenWidth = Screen.Width / _
Screen.TwipsPerPixelX
nScreenHeight = Screen.Height / _
Screen.TwipsPerPixelY
'Get Peedy's width and height (in pixels)
nPeedyWidth = Peedy.Width
nPeedyHeight = Peedy.Height
'Move Peedy to bottom-right corner of
'screen
Peedy.MoveTo nScreenWidth - _
nPeedyWidth, nScreenHeight - _
nPeedyHeight
End Sub
Private Sub cmdProcess_Click()
Peedy.Play "Process"
End Sub
Private Sub cmdShow_Click()
Peedy.Show
End Sub
Private Sub cmdSpeak_Click()
Peedy.Speak txtSpeech
End Sub
Private Sub cmdWave_Click()
Peedy.Play "Wave"
Peedy.Play "RestPose"
End Sub
Private Sub Form_Load()
Agent1.Characters.Load "Peedy", _
"peedy.acs"
Set Peedy = _
Agent1.Characters.Character("Peedy")
End Sub
Private Sub Form_Unload(Cancel As Integer)
Agent1.Characters.Unload "Peedy"
End Sub
|
|