.NET Zone      
 
Smack the Santa: Creating a Game in VB.NET (cont.)

The Popup Mechanism
The timer provides regularly occurring events, now you can make something happen. Add the HoHo function, which control the Santas' visibility.

   Sub HoHo(ByVal tTicker As Object, _
      ByVal ePopup As EventArgs)
      If bOn Then
         arrSantas(iThisSanta).Visible = False
         bOn = False
      Else
         iThisSanta = rAndy.Next(0, iSantaCount)
         arrSantas(iThisSanta).Image = Me.btnSanta.Image
         arrSantas(iThisSanta).Visible = True
         bOn = True
      End If
   End Sub
To make sense of the HoHo method, you should know how the dynamic form displays the Santas. You have a couple of different options for randomly generating controls on a new form. I decided that the most straightforward technique would be to create a matrix of invisible Santa buttons. The row and column counts specified in the Control Form control the number and arrangement of buttons on the dynamic playing field form. The code switches those buttons on or off during the game, making a Santa appear or disappear when the timer fires. Don't worry—I cover all of this in more detail further in this tutorial.

Again, you must include the two arguments: Object and EventArgs. In other applications, you might actually use these arguments, but in this demo, I include them only because they are required for all event handlers.

As you can see, the HoHo routine uses the global Boolean bOn variable to determine whether a Santa appears or disappears when the timer ticks. On the first tick, bOn is False, so a Santa appears. Two seconds later, that Santa will disappear—whether it's been smacked or not. The HoHo method makes the Santa appear and disappear by switching the Visible property of the appropriate button to True or False.

To make the Santa appear in a random place on the screen, the project uses a nifty new class called Random. This class, which is part of the .NET Framework, handles all the random number generation previously (and currently) implemented by classic VB's Rnd function. Instead of forcing you to write sloppy range formulas and other subtle messiness associated with random numbers, the Random class includes properties and methods that can handle all your random needs cleanly and elegantly. The HoHo method calls the Next property of the global variable rAndy (the Random object) and sends it the allowable range of numbers. The Next property returns the next random number in the sequence, which the code uses as the index into the Santa array. That member of the Santa array is then switched "on" so it's available for smacking.


Global Variable Declarations

Smack That Bad Boy!

Introduction The Smack The Santa Control Form Global Variable Declarations
The Popup Mechanism Smack That Bad Boy!  


 





 TALK BACK
Have the problems associated with browser incompatibilities, technology advances, and .NET killed off DHTML and browsers for distributed application development? Let us know in the talk.editors.devx discussion group.
Click here to Join
Get the code for this article

The .NET Framework SDK

.NET Events

Grouping Data in Collections





 
Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers

Copyright Information/Privacy Statement