Now we need to add some more to the app to make everything move and draw it to the screen. In my example I have both the ways of doing it in the same app. It is a label on the top that tells you what way is used at the moment. If you click it will use the other method. It is two different timers that use the different methods. The code to change between the timers, and to change the :
Private Sub lblMethod_Click()
'If clicked then use the other method, and print it on the label
If lblMethod.Caption = "Using Key Events" Then
tmrArray.Enabled = False
tmrAPI.Enabled = True
lblMethod.Caption = "Using GetAsyncKey"
lblMethod.Caption = "Using Key Events"
tmrAPI.Enabled = False
tmrArray.Enabled = True
I have called the first timer for tmrArray. When this one is running it is the first method that is used to check what key is pressed. The other timer is called tmrAPI, and when this one is running the GetAsynkKeyState version is running. No more fancy there. The default will be the Array method. I am setting it to the default in the Form Load event with this code:
'Setting the interval of the "game loop"/timer to 10 for the API version
tmrAPI.Interval = 10
'Setting the interval of the "game loop"/timer to 10 for the Array version
tmrArray.Interval = 10
'Makint the array version the default at start up.
tmrArray.Enabled = True
The intervals will be 10 for both. So that means that it will check keypresses 100 times a second. It might not work that fast for your PC, but you can change it as you want. We also need to set some properties for the form. And we also need to draw the objects before anything else happens. Here is my code:
'Setting some properties for the Form,
' and are painting the first two pictures
.AutoRedraw = True
.KeyPreview = True
.PaintPicture imgSmily1.Picture, iX, iY
.PaintPicture imgSmily2.Picture, iX2, iY2
We are setting the AutoRedraw property to true. This is because if the window is getting resized or closed the drawings will disappear if we don't do it. We are setting the KeyPreview so we can trap the keyboard presses and releases. The two next lines, are actually function calls. They are painting the pictures to the form. I have two image boxes on the form called imgSmiley1 and imgSmiley2. I am sending the picture property and the X and Y coordinates for the two objects as parameters, and that will draw the smilies to the form. Just remember to have the visible property of the image boxes set to false. After each "frame"/"move" we have to draw them again. But first we have to clear everything that we have painted on the form, we do that by adding more or less the same lines at the end after checking for key presses in both the tmrAPI and the tmrArray events. The code for doing that is:
'Clears the screen
'Paints the first smilie
Me.PaintPicture imgSmily1.Picture, iX, iY
'Paints the second smilie
Me.PaintPicture imgSmily2.Picture, iX2, iY2
The first line is clearing the form, and the two next are drawing them to the new position. That’s all you need to do. you can try to make the app your self, or you can download the version that I have made here in the attachment. Hope you learned something new.