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

Global Variable Declarations
After generating the control form, you can begin building the game functionality. Start by declaring the global variables at the class level rather than within any procedures. If you're new to VB.NET, you might take particular pleasure in VB.NET's ability to both instantiate a variable and assign a value to it in a single line of code. Also note how the code below declares iRows and iCols. In classic VB, this syntax would create a Variant and an Integer, but VB.NET now creates two Integers.

       Dim iSantaCount As Integer = 0
       Dim iSmacked As Integer = 0
       Dim iThisSanta As Integer = 0
       Dim arrSantas() As Button
       Dim sbStatus As StatusBar
       Dim rAndy As New System.Random()
       Dim bOn As Boolean = False
       Dim iRows, iCols As Integer
       Const TWIDTH As Integer = 100
       Const THEIGHT As Integer = 100
Add a Timer Control
You need a way to make the Santas pop up at random on the screen. Because the Santas should appear and disappear at regular intervals, a Timer control works perfectly.

   Sub StartClock(ByVal frmChimney As Object, _
      ByVal eStuff As EventArgs)
    
      Dim tTick As New Timer()
      tTick.Interval = 2000 / tbSpeed.Value
      AddHandler tTick.Tick, AddressOf HoHo
      tTick.Start()
   End Sub
The StartClock method has several important elements. First, because StartClock is an event procedure, it requires two arguments: an Object variable and an EventArgs variable. Usually, the IDE generates event handler methods and these required arguments when you double-click on the control in the designer or add an event handler from the drop-down event list, but in this case—because the procedure handles the Load event of the dynamic form—you must add them manually. I stress this because I consistently forget to add these bloody arguments and tend to generate a fair number of errors in this portion of the code. I hope my pain saves you some time.

Second, the snippet creates a Timer control, which is the first line in the body of the procedure. A useful Timer needs two things: an interval between "ticks" and something to do when a tick occurs. The second line in the StartClock function body defines the interval, in milliseconds, based on the value of the Speed slider in the control form. In this case, the maximum interval between ticks will be 2 full seconds. The minimum value will be 400 ms or .4 seconds.

The AddHandler line gives the Timer something to do on those ticks. Specifically, it assigns the HoHo procedure to handle the timer's Tick event. With one simple AddHandler statement, you can associate any procedure with any event through the magic of the AddressOf operator. To oversimplify things, AddressOf creates a kind of pointer to procedures—it returns the memory address of the procedure it accepts as an argument. Using AddressOf in conjunction with the AddHandler statement lets you associate an arbitrary procedure to an event. Pulling it all together, here's the generic syntax to add an event handler. You'll see in action later on when you create the Santa buttons:

           AddHandler Object.Event, AddressOf Procedure
Finally, the code starts the timer, setting the program in motion to begin displaying Santas.


The Smack The Santa Control Form

The Popup Mechanism

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