Results 1 to 7 of 7

Thread: [RESOLVED] Game movement keydown event

  1. #1

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Resolved [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.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: Game movement keydown event

    how would you use threading in a key_down event?

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    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.

  7. #7

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    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
  •  



Click Here to Expand Forum to Full Width