binderben 09-26-2005, 06:24 AM Hi everyone,
I am trying to get diagonal movement working for my rpg this is the code i am using for the keydown event.
Select Case e.KeyCode
Case Keys.Up
If Not alex.InMotion Then alex.MoveUp()
Case Keys.Up & Keys.Left
If Not alex.InMotion Then alex.MoveUpLeft()
Case Keys.Up & Keys.Right
If Not alex.InMotion Then alex.MoveUpright()
Case Keys.Down & Keys.Right
If Not alex.InMotion Then alex.Movedownright()
Case Keys.Down & Keys.Left
If Not alex.InMotion Then alex.Movedownleft()
Case Keys.Down
If Not alex.InMotion Then alex.MoveDown()
Case Keys.Left
If Not alex.InMotion Then alex.MoveLeft()
Case Keys.Right
If Not alex.InMotion Then alex.MoveRight()
Case Keys.Add
If Not alex.InMotion Then IncreaseSpeed()
Case Keys.Subtract
If Not alex.InMotion Then DecreaseSpeed()
The problem Lies with the diagonal parts alex.movedownleft, alex.moveupright etc..
I was wondering if I was doing it right by saying
Case Keys.Down & Keys.Left?
Thanks :)
noi_max 09-26-2005, 09:00 AM Is this .NET?
How are things moving around now? Just with the keypresses? Or do you have some kind of timer function?
A similar question was asked recently about keydown events and some links given, although they are VB6 related.
http://www.visualbasicforum.com/showthread.php?t=238806
binderben 09-26-2005, 09:19 AM It is in .net and yes just keypresses
noi_max 09-26-2005, 11:42 AM Hmm.. well you may be able to use some of the concepts in the basic movement tutorial link in the thread posted but the syntax will undoubtedly be different.
The basic idea is to have a running loop that checks values that are changed in the keydown and keyup events. For simplicity, a timer control was used to have this 'main loop' happening at specific time intervals.
The thread was moved to the .NET section. Perhaps those that are fluent in .NET can have a look :)
Machaira 09-26-2005, 11:54 AM See if the attached helps out.
killerguppy101 09-26-2005, 07:53 PM one way that i prefer to do key based movements is have an array of booleans called key_down(255) as boolean. on the key down and key up events i place code to turn those keys on/off. ie-
key_down(e.keycode)=true/false
then i have a main loop that each frame checks the keys pressed, updates the player position, and draws the scene. to move the player, i usually have something like this:
if key_down(keys.right) then
player.x=player.x+player.speed/fps
end if
player.speed is measured in pixels/second and the fps is just a count of how many frames are completed in a second. this keeps the speed of the player constant, on slow computers and fast ones. it also provides a much nicer unit of speed (pixels/second, instead of pixels/frame, which are usually 0.05 or some small hard to work with number)
MikeJ 09-26-2005, 08:29 PM Well, I'm not 100% sure, but shouldn't it be:
Case Keys.Up And Keys.Left
'etc
Iceplug 09-27-2005, 07:07 AM First of all, e.KeyCode can only be one key.
It can only be left or up (or another among the remaining keycodes as well), it cannot be more than one keycode at once.
Second of all, Keys.Up And Keys.Left is a boolean operation (which would evaluate roughly to 36, that's the Home key) that won't net you the key that you want.
Zumwalt 09-27-2005, 09:42 AM Angle of movement can be determined by using a simple formula to determine angle facing.
What I mean by that, is draw a circle, put numbers like a clock around the circle, if they press the left key, subtract the face by 1 point (or 2 or 3, or whatever), and draw the image facing that point in the circle, movement will be now in the direction of that point.
If you only want to work with 8 points, on a 12 point circle, exactly like a clock, 12 is up, 6 is down 3 is right, 9 is left, so if you want up right, you will need to update the point to 1 or 2 or change the system to an 8 point circle, 8 is up, 4 is down 2 is right 6 is left.
So, this frees up your up and down key press to velocity keys.
IF keypress is right and face is 8, then face becomes 1 (up right angle) movement is not modified, velocity can increase with keypress up, or keypress down to min / max, or STEP.
End result is key layout of:
781
602
543
0 represents image center.
Keypress left and right control + / - to face
Keypress up and down represent + / - to velocity or step, so now they can be facing 1, keypress down, flip facing to 5 and step = 1.
Make sense?
Edit:
Say you wanted to stick with single key press for angle movement, and you only can deal with one key at a time, use the keypad.
789
456
123
Where 7 is up left, 9 is up right, 1 is down left, 3 is down right, etc.
jo0ls 09-28-2005, 08:50 PM Or you could use directinput, which isn't as horrendous as it sounds. It will poll the keyboard at the game tick and collect the status of all keys at that point in time.
I was wondering why I got beeps when pressing 3 keys in Machaira's example. I made a keyboard hook, which beeped, then the attached program which does it too. Then I tried a different keyboard and that sorted it, lol.
Machaira 09-29-2005, 07:40 AM DirectInput would be the cleaner solution IMO, but for a quick and dirty solution I think mine works. :)
Iceplug 09-29-2005, 06:09 PM I think the easiest way would be to simply do as killerguppy said and have an array of booleans that indicate if a key is pressed.
Dim KeysDown(255) As Boolean
In KeyDown
KeysDown(Convert.ToInt32(e.KeyCode)) = True
In KeyUp
KeysDown(Convert.ToInt32(e.KeyCode)) = False
And then in a timer or something to check the keys:
If KeysDown(38) Then 'Up.
Alex.moveup
ElseIf KeysDown(40) Then 'Down.
Alex.movedown
End If
If KeysDown(37) Then 'Left
Alex.moveleft
ElseIf KeysDown(39) Then 'Right
Alex.moveright
End If
And, as you can see, if KeysDown(37) and KeysDown(38) are true then moveup and moveleft will both be executed.
:)
|