|
-
Feb 26th, 2010, 12:17 AM
#1
Thread Starter
Lively Member
[RESOLVED] Game movement keydown event
Hey guys, i recently took a look at a game i tried to put together, and decided i wanted to finish it. The game is a shooter game that creates 'walls' that the player should not be able to move into. These walls are panels, and currently, im using one keydown event to move both players. This is not a good thing, because it makes it impossible for both players to shoot and move at the same time. The player should not be able to run through the walls. Any suggestions on how i can create two form_keydown events, i would appreciate the help.
-
Feb 26th, 2010, 12:35 AM
#2
Re: Game movement keydown event
That's a good question.Haven't ever tried something like that.I don't think overloading would work,Maybe threading?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 26th, 2010, 12:37 AM
#3
Thread Starter
Lively Member
Re: Game movement keydown event
how would you use threading in a key_down event?
-
Feb 26th, 2010, 12:12 PM
#4
Re: Game movement keydown event
No the whole application with treading.That's why i say it's a good question because i don't have a solid answer
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 26th, 2010, 06:44 PM
#5
Re: Game movement keydown event
Don't use the key_down event. Use the GetKeyState API and use it on a timer set to 1 millisecond (shortest interval for maximum move speed, you can always make the speed slower but using 1 millisecond will allow you to see the maximum).
Declare at top of form
Code:
Private Declare Function GetKeyState Lib "user32" (ByVal keyCode As Integer) As Integer
Use to detect keypress on timer tick
Code:
If GetKeyState(Keys.Left) < 0 Then 'Move Left, x + 1 whatever
I use this whenever I make a game that involves motion and it works like a charm. It not only avoids the key repeat delay that windows usually has but it supports multiple key presses. Keep in mind that past 3 or 4 keys no amount of coding will detect all the key presses as keyboards are only made to input 3 or 4 hits at a time. Past that you have to have expensive special key boards to be able to pick up more than 4 keys at a time.
Here's an example of what GetKeySync can do though.
http://www.mediafire.com/?o4jzq0zdcmn
I've tried it with 4 players and we were all able to move fine even though we were holding down our left or right move key at the same time. Also the motion is completely smooth.
-
Feb 27th, 2010, 03:16 PM
#6
Thread Starter
Lively Member
Re: Game movement keydown event
Thanks! this works really well. btw, would u know how to set boundaries? so that if the player moves out of bounds, they simply cannot move more? (for walls and the edge of the form)
EDIT
hm i realized that i still had the keydown event. the timer seems that it isnt finding any keys being pressed. u sure this works
DOUBLE EDIT
You have to use a short, not an integer. so something like this
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function GetKeyState(ByVal virtualKeyCode As Integer) As Short
End Function
Then import System.Runtime.InteropServices
It works now. :]
Last edited by manbearpig001; Feb 27th, 2010 at 03:59 PM.
-
Feb 27th, 2010, 05:39 PM
#7
Thread Starter
Lively Member
Re: Game movement keydown event
haha k so lots of progress right here.
Ive managed the boundaries completely, and the movement. i have a few things i have to take care of, but for now im good. Thanks a lot for that little push. it was exactly what i needed.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|