Tutorial [Move with Keypress] NMs Game tutorial 0.1
This is a tutorial to show you two ways of moving objects on a form in VB. The code has only been tested in VB6, but I guess it can be used for VB5 and easily implemented in .Net too. You should have some knowledge of arrays, and some knowledge of simple VB programming from before. It is not recommended that you know anything about APIs but you can manage the second bit a bit better if you know it. But if you have never used it at all. Don't turn around. The two ways I am going to show you are more or less equal when it comes to performance. The first way is (the way I did it for small games) by using a array for checking what keys that are hold down. The second method I will show is using an API call. But since this is not improving the speed in VB, I don't like to use it because it looks more complicated to use API calls for persons that are not used to it. I will only talk about the code here. And make a final example using both methods that you can download and play with in the end of this tutorial. So here we go.
Re: Tutorial [Move with Keypress] NMs Game tutorial 0.1
Excellent tutorial, and i may be out of place replying to it, but i have only one thing to add. To get smoother motion which will slide and can have different speeds, I came up with this very simple sequence.
In the general section, declare 2 more variables:
VB Code:
Dim xspeed as Integer
Dim yspeed as Integer
Then instead of modifying the left and top properties of the object modify the 2 variables:
VB Code:
If keyarray(37) = True Then xspeed = xspeed - 1
If keyarray(38) = True Then yspeed = yspeed - 1
If keyarray(39) = True Then xspeed = xspeed + 1
If keyarray(40) = True Then yspeed = yspeed + 1
Then modify the top and left properties according to the speed:
VB Code:
player.left = player.left + xspeed
player.top = player.top + yspeed
Finally, the other thing this is useful for is if you have boundaries for your moving object, this makes it easier to make things bounce off the wall. First, put in a line that will make sure it doesn't leave the boundary, then all you have to do is reverse the direction, and if you want, halve it to make it more realistic.
VB Code:
If player.left < 0 then
player.left = 0
xspeed = xspeed * -1 / 2
End If
If player.top < 0 then
player.top = 0
yspeed = yspeed * -1 / 2
End If
Hope this is helpful, i was pretty happy with myself when i made it work.
Re: Tutorial [Move with Keypress] NMs Game tutorial 0.1
I cannont find the list of keys on MSDN. Where are they? What are they?
Re: Tutorial [Move with Keypress] NMs Game tutorial 0.1
Quote:
Originally Posted by
Gamemaster1494
I cannont find the list of keys on MSDN. Where are they? What are they?
Use this VB 6.0 code