The PlayerWelcome routine, shown in Listing 14.15, is typical of what is required when you have interaction among multiple characters. The routine begins by introducing the game. I use the RequestWait variable to hold the result from the Speak method the host uses to introduce the first computer player. Then I play the “Pleased” action for the host.
Listing 14.15: PlayerWelcome Routine in I’m Thinking of a Color
Private Sub PlayerWelcome()
TheHost.Show
TheHost.MoveTo 325, 50, 1500
TheHost.Speak “Welcome to the I’m thinking of a color game.”
TheHost.Speak “I think of a color and three contestants try to guess it.”
TheHost.Speak “Player number one is a computer annimation from Microsoft.”
Set RequestWait = TheHost.Speak(“Let’s welcome “ & Player(1).Name & “.”)
TheHost.Play “Pleased”
Player(1).Wait RequestWait
Player(1).MoveTo 100, 100, 0
Player(1).Show
TheHost.GestureAt 100, 100
Player(1).MoveTo 100, 450, 1500
Player(1).Play “Greet”
TheHost.Play “Pleased”
TheHost.Play “PleasedReturn”
Set RequestWait = Player(1).Speak(“Hello “ & TheHost.Name & “.”)
Player(1).Play “GreetReturn”
TheHost.Wait RequestWait
TheHost.Speak “Player number two is also a computer annimation from “ & _
“Microsoft.”
Set RequestWait = TheHost.Speak(“Let’s give a big hand for “ & _
Player(1).Name & “.”)
TheHost.Play “Pleased”
Player(2).Wait RequestWait
Player(2).MoveTo 100, 100, 0
Player(2).Show
TheHost.GestureAt 100, 100
Player(2).MoveTo 550, 450, 1500
Player(2).Play “Greet”
TheHost.Play “Pleased”
TheHost.Play “PleasedReturn”
Set RequestWait = Player(1).Speak(“Hi “ & TheHost.Name & “.”)
Player(2).Play “GreetReturn”
TheHost.Wait RequestWait
TheHost.Speak “Player number three is a human from outside this computer.”
TheHost.Speak “Let’s welcome “ & Text2.Text & “.”
TheHost.Play “Pleased”
Set RequestWait = TheHost.Speak(“Are you ready to play?”)
Player(1).Wait RequestWait
Player(1).Play “Pleased”
Set RequestWait = Player(1).Speak(“Yes. Let’s go!”)
Player(2).Wait RequestWait
Player(2).Play “Pleased”
Set RequestWait = Player(2).Speak(“I’m ready!”)
Player(1).Play “PleasedReturn”
Player(2).Play “PleasedReturn”
YesFlag = False
Do While Not YesFlag
DoEvents
Loop
TheHost.Wait RequestWait
End Sub
The first thing I do for player one is make that player wait until the host has finished his introduction. I do this by using the Wait method with the RequestWait object. Then I perform a series of animations and speeches, ending in player one setting the RequestWait object. The host then uses the Wait method with the RequestWait object.
When studying the code, remember that most of the character’s methods merely place a request in the Agent server’s queue for that character. The requests in the queue will be executed one at a time until the queue is empty. Without the first Wait by player one, that player’s first action will be placed in the queue and executed immediately. Thus, while the host was still introducing the game, player one would arrive on the screen and start moving around.
With the Wait method, player one’s MoveTo and Show methods will not begin until the host has finished introducing player one. Likewise, the host will wait until player one has finished saying hello before introducing player two.
Interacting with the human is a little more difficult since there isn’t a Wait method available. Also, all of the user commands are received in the Command event, not through a regular function like InputBox. This means that processing user input is a bit more complicated.
However, there’s always the DoEvents routine. Remember that in the Command event, I set the variable YesFlag to True whenever the user said yes. I’m going to sit in a tight spin loop while the YesFlag is False. I stick in a call to DoEvents inside the loop, so that the system is free to receive and process the user command.
TIP: Waiting with spin loops: There are many times when you need to wait for a different process to complete before continuing with the current process. One way of waiting is to perform a loop on a global variable that another part of the program will change. This loop is called a spin loop. However, since Windows 95 is not a true multitasking system, this loop will lock up your system. To correct this limitation, just put a call to DoEvents inside the loop. This will return control back to Windows, so it can continue processing other work, including the part you’re waiting on.