Results 1 to 4 of 4

Thread: Simple Key Down Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Simple Key Down Question

    Hey so I just wanted to start making a simple 2D game, but I can't figure out how to make the character move! Yeah I know I'm pretty dumb but can you bear with me here? Thanks.

    So I want to make the character move with WASD. My problem is that when I press a key, nothing happens. Nothing at all. What am I doing wrong? Thanks for the help in advance.

    Code:
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    
            If e.KeyCode = Keys.W Then
                Player.Top = Player.Top - 10
                Label1.Text = "W"
            End If
            If e.KeyCode = Keys.A Then
                Player.Left = Player.Left - 10
                Label1.Text = "A"
            End If
            If e.KeyCode = Keys.S Then
                Player.Top = Player.Top + 10
                Label1.Text = "S"
            End If
            If e.KeyCode = Keys.D Then
                Player.Left = Player.Left + 10
                Label1.Text = "D"
            End If
    
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Simple Key Down Question

    in Form_Load try putting:

    Code:
    Me.KeyPreview = True

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Simple Key Down Question

    Wow, it works! Thanks!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Key Down Question

    Quote Originally Posted by .paul. View Post
    in Form_Load try putting:

    Code:
    Me.KeyPreview = True
    While it's not wrong to do that in code, I would tend to set properties like that in the designer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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