Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Go Back  Xtreme Visual Basic Talk > > > Able to have 2 Private Sub search_Keydown ?


Reply
 
Thread Tools Display Modes
  #1  
Old 01-01-2011, 06:26 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default Able to have 2 Private Sub search_Keydown ?


Hi, I need to in someway make this function individual, as I have two objects on the screen, and don't want particular buttons interfering with P2 controls.

You see I have a snake game, trying to make it 2 players, but every time one of the buttons i assigned is pressed it activates on both snakes. Now is there any way to make 2 individual search_keydown private subs, to allow simultaneous input from both P1 and P2 controls?

Thanks in advance.
Reply With Quote
  #2  
Old 01-01-2011, 07:52 PM
passel's Avatar
passelAble to have 2 Private Sub search_Keydown ? passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Well, not being able to see the code, can't say for sure, but normally you wouldn't want the repeated keydown timing of the button to control movement, but rather watch for the keydown event, and set a variable that the snake movement would look at each pass. The snake movements would occur in a "game loop".
If you set KeyPreview to True for the Form, the form can handle most keystrokes, setting the values that the snake movement code would look at and react to.

If that isn't clear, here's a very quick, simple, example.
Just add two buttons and a timer control to the form.
The buttons will start moving up when the code starts. Use the W,A,S and D keys to control button1, I,J,K and L keys to control button2.
They should be controllable at the same time.
Code:
Public Class Form1

  Dim Player1_Dir As Integer
  Dim Player2_Dir As Integer

  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
      '  Player 1
      Case Keys.W : Player1_Dir = 0
      Case Keys.A : Player1_Dir = 1
      Case Keys.S : Player1_Dir = 2
      Case Keys.D : Player1_Dir = 3

        'Player 2
      Case Keys.I : Player2_Dir = 0
      Case Keys.J : Player2_Dir = 1
      Case Keys.K : Player2_Dir = 2
      Case Keys.L : Player2_Dir = 3
    End Select

  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Interval = 50
    Timer1.Start()
  End Sub

  Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'Player1
    With Button1                 'Move the button in the current direction
      Select Case Player1_Dir
        Case 0 : .Top -= 1
        Case 1 : .Left -= 1
        Case 2 : .Top += 1
        Case 3 : .Left += 1
      End Select

      'Limit the button movement so it doesn't go off the form
      If .Top < 0 Then .Top = 0
      If .Bottom > Me.ClientSize.Height Then .Top = Me.ClientSize.Height - .Height
      If .Left < 0 Then .Left = 0
      If .Right > Me.ClientSize.Width Then .Left = Me.ClientSize.Width - .Width
    End With

    'Player2
    With Button2
      Select Case Player2_Dir
        Case 0 : .Top -= 1
        Case 1 : .Left -= 1
        Case 2 : .Top += 1
        Case 3 : .Left += 1
      End Select
      If .Top < 0 Then .Top = 0
      If .Bottom > Me.ClientSize.Height Then .Top = Me.ClientSize.Height - .Height
      If .Left < 0 Then .Left = 0
      If .Right > Me.ClientSize.Width Then .Left = Me.ClientSize.Width - .Width
    End With
  End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 01-01-2011 at 08:21 PM.
Reply With Quote
  #3  
Old 01-01-2011, 08:39 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Question

hmmmm thanks but are my directionx and y variables wrong? here's my code:

PS: i'm using arrow keys and wasd

Code:
    Private Sub search_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

            If e.KeyCode = Keys.Up Then
            Timer1.Enabled = True ' movement 
            basic_score.Enabled = True ' starts basic score (adds 3 every 90 intervals)
            Speed_y = -10
            Speed_X = 0
            directiony = 1
        End If


        If e.KeyCode = Keys.Down Then
            Timer1.Enabled = True
            basic_score.Enabled = True
            Speed_y = 10
            Speed_X = 0
            directiony = 1
        End If


        If e.KeyCode = Keys.Right Then
            Timer1.Enabled = True
            basic_score.Enabled = True
            Speed_y = 0
            Speed_X = 10
            directionx = 1
        End If

        If e.KeyCode = Keys.Left Then
            Timer1.Enabled = True
            basic_score.Enabled = True
            Speed_y = 0
            Speed_X = -10
            directionx = 1
        End If



        'Player 2 Movement


        If e.KeyCode = 87 Then
            P2movement.Enabled = True ' movement 
            basic_score.Enabled = True ' starts basic score (adds 1 every 90 intervals)
            Speed_y = -10
            Speed_X = 0
            directiony = 1
        End If


        If e.KeyCode = 83 Then
            P2movement.Enabled = True
            basic_score.Enabled = True
            Speed_y = 10
            Speed_X = 0
            directiony = 1
        End If


        If e.KeyCode = 68 Then
            P2movement.Enabled = True
            basic_score.Enabled = True
            Speed_y = 0
            Speed_X = 10

            directionx = 1
        End If

        If e.KeyCode = 65 Then
            P2movement.Enabled = True
            basic_score.Enabled = True
            Speed_y = 0
            Speed_X = -10
            directionx = 1
        End If

    End Sub
Reply With Quote
  #4  
Old 01-03-2011, 10:25 PM
passel's Avatar
passelAble to have 2 Private Sub search_Keydown ? passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

I only see one set of Speed_X and Speed_Y variables.
You need to have and X and Y for player 1 and another X and Y for player 2, ie.
Player1_Speed_X, Player1_Speed_Y, Player2_SpeedX, etc..
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 01-05-2011, 06:18 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by passel View Post
I only see one set of Speed_X and Speed_Y variables.
You need to have and X and Y for player 1 and another X and Y for player 2, ie.
Player1_Speed_X, Player1_Speed_Y, Player2_SpeedX, etc..
yeah i tried that, it still doesn't have independent movement on both snakes.

Because the search_keydown is picking up wasd and arrow keys and applying em to both snakes for some reason. I need to further work out another way to carry out the controls :/

I was thinking using key_press for Player 2, but then that would be kinda awkward because the controls will have to be used differently for P2 (then will have to keep holding the button to move) It would work, but as i said it won't have uniform controls :/

I know there has to be a way for search_keydown ... there has to be.
Reply With Quote
  #6  
Old 01-05-2011, 06:47 PM
AtmaWeapon's Avatar
AtmaWeaponAble to have 2 Private Sub search_Keydown ? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I don't think you implemented passel's advice properly. Usually it's best to back up "I tried that" with the code you tried.

The "for some reason" that search_keydown applies the keypress to both snakes is because you have one set of variables. If you only have one set of speed variables, and both snakes use them, then changing the speed variable will change how both snakes move. If you have two sets of speed variables and both snakes use *only* the correct variables, you won't have this problem.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 01-05-2011, 07:24 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
I don't think you implemented passel's advice properly. Usually it's best to back up "I tried that" with the code you tried.

The "for some reason" that search_keydown applies the keypress to both snakes is because you have one set of variables. If you only have one set of speed variables, and both snakes use them, then changing the speed variable will change how both snakes move. If you have two sets of speed variables and both snakes use *only* the correct variables, you won't have this problem.
here is my new code, two separate variables right. still doesn't work ><

Code:
  Private Sub search_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

Note: I have two timers running, one for P! snake and one for P2 snake, P1 timer is activated upon any arrow keypress, P2 timer is activated upon any of the wasd keys. once they are both enabled, this is where my problem begins. I cannot get the keydown function to work simultaneously to provide independent input of controls. meaning that any arrow keys and wasd keys control both snakes in the same directions. Am i missing a direction variable or something? I am starting to see this as impossible to implement now. :/ please help.


        If e.KeyCode = Keys.Up Then
            P1movement.Enabled = True ' movement 
            basic_score.Enabled = True ' starts basic score (adds 3 every 90 intervals)
            Player1_Speed_Y = -10
            Player1_Speed_X = 0

        End If


        If e.KeyCode = Keys.Down Then
            P1movement.Enabled = True
            basic_score.Enabled = True
            Player1_Speed_Y = 10
            Player1_Speed_X = 0

        End If


        If e.KeyCode = Keys.Right Then
            P1movement.Enabled = True
            basic_score.Enabled = True
            Player1_Speed_Y = 0
            Player1_Speed_X = 10

        End If

        If e.KeyCode = Keys.Left Then
            P1movement.Enabled = True
            basic_score.Enabled = True
            Player1_Speed_Y = 0
            Player1_Speed_X = -10

        End If



        'Player 2 Movement


        If e.KeyCode = 87 Then
            P2movement.Enabled = True ' movement 
            basic_score.Enabled = True ' starts basic score (adds 1 every 90 intervals)
            Player2_Speed_Y = -10
            Player2_Speed_X = 0

        End If


        If e.KeyCode = 83 Then
            P2movement.Enabled = True
            basic_score.Enabled = True
            Player2_Speed_Y = 10
            Player2_Speed_X = 0

        End If


        If e.KeyCode = 68 Then
            P2movement.Enabled = True
            basic_score.Enabled = True
            Player2_Speed_Y = 0
            Player2_Speed_X = 10

        End If

        If e.KeyCode = 65 Then
            P2movement.Enabled = True
            basic_score.Enabled = True
            Player2_Speed_Y = 0
            Player2_Speed_X = -10

        End If

    End Sub
P1 and P2 are running on 2 different timers, if that helps (i did type a whole paragraph here, but i pressed 'post reply button and everything disappeared) [I hate forums].


hmm i copied passel codes and put it in a new vb project to test it, added 2 buttons and 1 timer, but the buttons don't respond to any input, they just keep going up until they hit the boarder :/

ahahaha EDIT: I fixed IT!!, Yeah thanks guys, really appreciated your help It turns out i forgot to adjust those variables elsewhere as well, but quickly changed it and now i have INDEPENDENT movement thanks once again. made my day

Last edited by 45minutes; 01-05-2011 at 10:01 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ? Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
 
Able to have 2 Private Sub search_Keydown ?
Able to have 2 Private Sub search_Keydown ?
 
-->