Movement control issues

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1310 Views - Last Post: 10 June 2015 - 05:11 PM Rate Topic: -----

#1 Sage1993   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-June 15

Movement control issues

Posted 08 June 2015 - 07:00 AM

Good morning everyone,

I woke up and loaded my project in Visual studio 2012, and for some reason my arrow left and right are not responding.
I'm having some problems debugging and I cant find out why the keys aren't working.

/code:

Private Sub Game_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

If e.KeyCode = Keys.Left Then
MoveLeft = True
end if

If e.KeyCode = Keys.Right Then
MoveRight = True
end if

end sub

Private Sub Game_KeyUp(sender as Object, e As KeyEventArgs) Handles Me.KeyUp

If e.KeyCode = Keys.Left Then
MoveLeft = False
end if

If e.KeyCode = Keys.Right Then
MoveRight = False
end if 

end sub

Private Sub PMove_Tick(sender As Object, e As EventArgs) Handles PMove.Tick

If MoveLeft = True Then
picPlayer.left -= 10

If picPlayer.Left <= 0 then
picPlayer.Left = 0
end if
endif

If MoveRight = True then
picPlayer.Left += 10

If picPlayer.Left + 40 >= Me.Width Then
picPlayer.Left = Me.Width - 40
end if
end if
end sub


/Code


My apologies snips couldn't upload...
MoveLeft/MoveRight are Public in a Module declared as Booleans.
Timer is set to enabled.true, and the interval is 50

Thank you for the assistance,
Sage1993

This post has been edited by modi123_1: 08 June 2015 - 07:10 AM
Reason for edit:: Please use the 'code' button in the editor to apply code tags for readability.


Is This A Good Question/Topic? 0
  • +

Replies To: Movement control issues

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: Movement control issues

Posted 08 June 2015 - 07:11 AM

Have you put breakpoints in the critical areas? Verify they are being triggered?
Was This Post Helpful? 0
  • +
  • -

#3 Sage1993   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-June 15

Re: Movement control issues

Posted 08 June 2015 - 07:17 AM

Yes I have, the issue came up that I couldn't trigger the keys, because I also have moving objects going on so, I don't know necessarily what to do...
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: Movement control issues

Posted 08 June 2015 - 07:27 AM

Did you turn on the form's "key preview" property?
Was This Post Helpful? 0
  • +
  • -

#5 Sage1993   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-June 15

Re: Movement control issues

Posted 08 June 2015 - 07:30 AM

I don't know what that is, or how to do that >.>...

View Postmodi123_1, on 08 June 2015 - 07:27 AM, said:

Did you turn on the form's "key preview" property?

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: Movement control issues

Posted 08 June 2015 - 08:05 AM

Look at your form in the designer. Click on the form (and not a control, panel, etc). The properties panel should show all the properties for the form. In the properties is 'key preview'. The default is false, but it sounds like you need it set to 'true'.

I am confused on how any of this was working beforehand.
Was This Post Helpful? 1
  • +
  • -

#7 Sage1993   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-June 15

Re: Movement control issues

Posted 08 June 2015 - 09:15 AM

I found the KeyPreview() = True
and set it in my code under the form1 load.
Everything works now, and I don't know why it was working before this. ^.^
Is this "KeyPreview" something I need to use every time now that I use keys to trigger events?


View Postmodi123_1, on 08 June 2015 - 08:05 AM, said:

Look at your form in the designer. Click on the form (and not a control, panel, etc). The properties panel should show all the properties for the form. In the properties is 'key preview'. The default is false, but it sounds like you need it set to 'true'.

I am confused on how any of this was working beforehand.

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: Movement control issues

Posted 08 June 2015 - 09:23 AM

It depends on the project, and you need to apply some critical thinking if you need to turn it on or not.

From what I am piecing together you want the entire form to react to key presses, right? If that is the case then you need to turn it on. If you want specific active controls to react to keypresses (and not the form as a whole) then you keep it off.
Was This Post Helpful? 0
  • +
  • -

#9 Sage1993   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-June 15

Re: Movement control issues

Posted 08 June 2015 - 10:09 AM

As of now, the code is a game I am creating with two players, controlled by 'A''D' and 'LeftArrow''RightArrow' with 'W' and 'Uparrow' triggering a object to move towards the other player, I do not plan on having any other key presses to need to be used besides those. Would I run into any errors if users pressed other keys during 'Runtime' of the program?

View Postmodi123_1, on 08 June 2015 - 09:23 AM, said:

It depends on the project, and you need to apply some critical thinking if you need to turn it on or not.

From what I am piecing together you want the entire form to react to key presses, right? If that is the case then you need to turn it on. If you want specific active controls to react to keypresses (and not the form as a whole) then you keep it off.

Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: Movement control issues

Posted 08 June 2015 - 10:13 AM

Quote

Would I run into any errors if users pressed other keys during 'Runtime' of the program?

I am unclear what errors you maybe referring to, but how about you try it out and see.

Side note - you do not need to quote the post above yours. The 'reply' is fine.
Was This Post Helpful? 0
  • +
  • -

#11 Sage1993   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-June 15

Re: Movement control issues

Posted 09 June 2015 - 09:15 AM

Below I have attached my progress in a game that I am creating in class as a final project.
Now I have received approval from my teacher to do this because we both cant figure this out.
Pictures range from 100KB to 300KB and there is about 9 pictures so far in the game including a 400KB background.
While the game is playing all the pictures are transparent so you can see the background, but when you go to shoot a
sword at the enemy player, followed by a movement command the game will slow down to a near halt.
I was wondering if there is any way of fixing this problem, and also I'm open to any changes in my code, if there is an easier way to do something. I'm willing to learn!!!

[
Public Class Game
    Private Sub Game_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        KeyPreview() = True
        DoubleBuffered = True
    End Sub
    Private Sub PMove1_Tick(sender As Object, e As EventArgs) Handles PMove1.Tick

        'Smooth the ticker
        DoubleBuffered = True
        'Movement, Weapon1, and Player1
        MovementPlayer1()
        'Sword Fire Player1
        ShotPlayer1()

    End Sub
    Sub MovementPlayer1()
        'Player
        'Left Movement, weapon and player
        If MoveLeft1 = True Then
            Player1.Left = Player1.Left - 5
            Reload1.Left = Reload1.Left - 5
            If Sword1Fire = False Then
                Sword1.Left = Sword1.Left - 5
            End If
            'Boundries on left of the form
            If Player1.Left <= 0 Then
                Player1.Left = 0
                Sword1.Left = 0
                Reload1.Left = 0
            End If
        End If

        'Right Movement, weapon and player
        If MoveRight1 = True Then
            Player1.Left = Player1.Left + 5
            Reload1.Left = Reload1.Left + 5
            If Sword1Fire = False Then
                Sword1.Left = Sword1.Left + 5
            End If
            'Boundries on the right of the form
            If Player1.Left + 40 >= Me.Width Then
                Player1.Left = Me.Width - 40
                Sword1.Left = Me.Width - 40
                Reload1.Left = Me.Width - 40
            End If
        End If
    End Sub
    Sub ShotPlayer1()
        'Shot/Reload Sword
        If Sword1Fire = True Then
            Sword1.Visible = True
            Sword1.Top = Sword1.Top + 10
            If Sword1.Top > 680 Then
                Sword1.Hide()
                Sword1Fire = False
                Sword1.Location = Reload1.Location
            End If
        End If

    End Sub
    Private Sub PMove2_Tick(sender As Object, e As EventArgs) Handles PMove2.Tick

        'Smooth the ticker
        DoubleBuffered = True
        'Movement, Weapon2, and Player2
        MovementPlayer2()
        'Sword Fire Player2
        ShotPlayer2()

    End Sub
    Sub MovementPlayer2()

        'Left Movement, Weapon and player
        If MoveLeft2 = True Then
            Player2.Left = Player2.Left - 5
            Reload2.Left = Reload2.Left - 5
            'Only move the shot if it isnt fired
            If Sword2Fire = False Then
                Sword2.Left = Sword2.Left - 5
            End If
        End If

        'Boundries on left of the form
        If Player2.Left <= 0 And Sword2.Left <= 0 And Reload2.Left <= 0 Then
            Player2.Left = 0
            Sword2.Left = 0
            Reload2.Left = 0
        End If

        'Right Movement, weapon and player
        If MoveRight2 = True Then
            Player2.Left = Player2.Left + 5
            Reload2.Left = Reload2.Left + 5
            'Only move the shot if it isnt fired
            If Sword2Fire = False Then
                Sword2.Left = Sword2.Left + 5
            End If
        End If
        'Boundries on the right of the form
        If Player2.Left + 40 >= Me.Width Then
            Player2.Left = Me.Width - 40
            Sword2.Left = Me.Width - 40
            Reload2.Left = Me.Width - 40
        End If

    End Sub
    Sub ShotPlayer2()
        'Shot/Reload Sword2
        If Sword2Fire = True Then
            Sword2.Visible = True
            Sword2.Top = Sword2.Top - 5
            If Sword2.Top < 0 Then
                Sword2.Hide()
                Sword2Fire = False
                Sword2.Location = Reload2.Location
            End If
        End If
    End Sub
    Private Sub Game_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        'Player 1 Movements
        If e.KeyCode = Keys.Left Then
            MoveLeft1 = True
        End If
        If e.KeyCode = Keys.Right Then
            MoveRight1 = True
        End If
        If e.KeyCode = Keys.Down Then
            Sword1Fire = True
        End If

        'Player 2 Movements
        If e.KeyCode = Keys.A Then
            MoveLeft2 = True
        End If
        If e.KeyCode = Keys.D Then
            MoveRight2 = True
        End If
        If e.KeyCode = Keys.W Then
            Sword2Fire = True
        End If


    End Sub
    Private Sub Game_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp

        'Player 1 Movements
        If e.KeyCode = Keys.Left Then
            MoveLeft1 = False
        End If
        If e.KeyCode = Keys.Right Then
            MoveRight1 = False
        End If

        'Player 2 Movements
        If e.KeyCode = Keys.A Then
            MoveLeft2 = False
        End If
        If e.KeyCode = Keys.D Then
            MoveRight2 = False
        End If



    End Sub
    Sub Ball_Player1()

        'Player 1 reaction to beachball
        If picBeachBall.Bounds.IntersectsWith(Player1.Bounds) Then
            VertDirect = -VertDirect
            HoriDirect = HoriDirect
            picBeachBall.Top = Me.Height - 620
        End If

        'Player 1 Reaction to basketball
        If picBasketBall.Bounds.IntersectsWith(Player1.Bounds) Then
            VertDirect1 = -VertDirect1
            HoriDirect1 = HoriDirect1
            picBasketBall.Top = Me.Height - 620
        End If

        'Player 1 Reaction to Baseball
        If picBaseball.Bounds.IntersectsWith(Player1.Bounds) Then
            VertDirect2 = -VertDirect2
            HoriDirect2 = HoriDirect2
            picBaseball.Top = Me.Height - 620
        End If
    End Sub
    Sub Ball_Player2()

        'Player 2 Reaction to basketball
        If picBasketBall.Bounds.IntersectsWith(Player2.Bounds) Then
            VertDirect1 = -VertDirect1
            HoriDirect1 = HoriDirect1
            picBasketBall.Top = Me.Height - 115
        End If

        'Player 2 Reaction to beachball
        If picBeachBall.Bounds.IntersectsWith(Player2.Bounds) Then
            VertDirect = -VertDirect
            HoriDirect = HoriDirect
            picBeachBall.Top = Me.Height - 115
        End If

        'Player 2 Reaction to Baseball
        If picBaseball.Bounds.IntersectsWith(Player2.Bounds) Then
            VertDirect2 = -VertDirect2
            HoriDirect2 = HoriDirect2
            picBaseball.Top = Me.Height - 115
        End If
    End Sub
    Sub Ball_Vs_Ball_Reaction()

        'Baseball & Beachball Reactions
        If picBaseball.Bounds.IntersectsWith(picBeachBall.Bounds) Then
            VertDirect2 = -VertDirect2
            HoriDirect2 = -HoriDirect2
            VertDirect = -VertDirect
            HoriDirect = -HoriDirect
        End If

        'Basketball & Baseball Reactions
        If picBaseball.Bounds.IntersectsWith(picBasketBall.Bounds) Then
            VertDirect2 = -VertDirect2
            HoriDirect2 = -HoriDirect2
            VertDirect1 = -VertDirect1
            HoriDirect1 = -HoriDirect1
        End If

        'BasketBall & BeachBall Reactions
        If picBasketBall.Bounds.IntersectsWith(picBeachBall.Bounds) Then
            VertDirect1 = -VertDirect1
            HoriDirect1 = -HoriDirect1
            VertDirect = -VertDirect
            HoriDirect = -HoriDirect
        End If


    End Sub
    Private Sub Beachball_Tick(sender As Object, e As EventArgs) Handles BeachBall.Tick

        'Smooth Timer
        DoubleBuffered = True

        'Movement
        picBeachBall.Left = picBeachBall.Left + (HoriDirect)
        picBeachBall.Top = picBeachBall.Top + (VertDirect)

        'Reactions to events
        Beachball_Reactions()

        'Reaction to players
        Ball_Player1()
        Ball_Player2()

        'Reaction to other balls
        Ball_Vs_Ball_Reaction()

    End Sub
    Sub Beachball_Reactions()
        'Beachball reaction to Game Walls
        'Right side of Game
        If picBeachBall.Left + 70 >= Me.Width Then
            picBeachBall.Left = Me.Width - 70
            HoriDirect = -HoriDirect
        End If
        'Bottom of Game
        If picBeachBall.Top + 85 >= Me.Height Then
            picBeachBall.Top = Me.Height - 85
            VertDirect = -VertDirect
        End If
        'Left side of Game
        If picBeachBall.Left <= 0 Then
            picBeachBall.Left = 0
            HoriDirect = -HoriDirect
        End If
        'Top of Game
        If picBeachBall.Top <= 0 Then
            picBeachBall.Top = 0
            VertDirect = -VertDirect
        End If

    End Sub
    Private Sub BasketBall_Tick(sender As Object, e As EventArgs) Handles BasketBall.Tick

        'Smooth Timer
        DoubleBuffered = True

        'Movement
        picBasketBall.Left = picBasketBall.Left + (HoriDirect1)
        picBasketBall.Top = picBasketBall.Top + (VertDirect1)

        'Reactions to events
        Basketball_Reactions()

        'Reaction to players
        Ball_Player1()
        Ball_Player2()

        'Reaction to other ball
        Ball_Vs_Ball_Reaction()

    End Sub
    Sub Basketball_Reactions()

        'Basketballs reaction to game walls
        'Right side of game
        If picBasketBall.Left + 70 >= Me.Width Then
            picBasketBall.Left = Me.Width - 70
            HoriDirect1 = -HoriDirect1
        End If

        'Bottom of Game
        If picBasketBall.Top + 85 >= Me.Height Then
            picBasketBall.Top = Me.Height - 85
            VertDirect1 = -VertDirect1
        End If

        'Left side of Game
        If picBasketBall.Left <= 0 Then
            picBasketBall.Left = 0
            HoriDirect1 = -HoriDirect1
        End If

        'Top of Game
        If picBasketBall.Top <= 0 Then
            picBasketBall.Top = 0
            VertDirect1 = -VertDirect1
        End If

    End Sub
    Private Sub Baseball_Tick(sender As Object, e As EventArgs) Handles Baseball.Tick
        'Smooth the timer
        DoubleBuffered = True

        'Movement of ball
        picBaseball.Left = picBaseball.Left + (HoriDirect2)
        picBaseball.Top = picBaseball.Top + (VertDirect2)

        'Reactions to Game Events
        BaseBall_Reactions()

        'Reactions to Player/Ball Events
        Ball_Player1()
        Ball_Player2()

        'Reaction to other ball
        Ball_Vs_Ball_Reaction()

    End Sub
    Sub BaseBall_Reactions()

        'Baseball reaction to walls of game
        'Right side of Game
        If picBaseball.Left + 70 >= Me.Width Then
            picBaseball.Left = Me.Width - 70
            HoriDirect2 = -HoriDirect2
        End If

        'Bottom of Game
        If picBaseball.Top + 85 >= Me.Height Then
            picBaseball.Top = Me.Height - 85
            VertDirect2 = -VertDirect2
        End If

        'Left side of Game
        If picBaseball.Left <= 0 Then
            picBaseball.Left = 0
            HoriDirect2 = -HoriDirect2
        End If

        'Top of Game
        If picBaseball.Top <= 0 Then
            picBaseball.Top = 0
            VertDirect2 = -VertDirect2
        End If

    End Sub
    Private Sub btnPause_Click(sender As Object, e As EventArgs)
        If PMove2.Enabled = True Then
            PMove2.Enabled = False
        ElseIf PMove2.Enabled = False Then
            PMove2.Enabled = True
        End If
        If BeachBall.Enabled = True And BasketBall.Enabled = True And Baseball.Enabled = True Then
            BeachBall.Enabled = False
            BasketBall.Enabled = False
            Baseball.Enabled = False
            MessageBox.Show("Click Ok to resume")
        ElseIf BeachBall.Enabled = False And BasketBall.Enabled = False And Baseball.Enabled = False Then
            BeachBall.Enabled = True
            BasketBall.Enabled = True
            Baseball.Enabled = True
        End If
        
    End Sub
End Class
]

:code:
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: Movement control issues

Posted 09 June 2015 - 10:00 AM

First off - you have too many timers. You need only *ONE* timer to act as the game engine. Each tick you go through your objects, have a common 'update' to run, and then draw them.

Example:
http://www.dreaminco...-basics-part-1/
Was This Post Helpful? 1
  • +
  • -

#13 Sage1993   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-June 15

Re: Movement control issues

Posted 09 June 2015 - 11:12 AM

Wow!!! Thank you so much.... I didn't even think about it. I'm in a fast paced class, and I'm kind of rushing through the program since we have 4 weeks in each language.
Was This Post Helpful? 0
  • +
  • -

#14 Sage1993   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-June 15

Re: Movement control issues

Posted 10 June 2015 - 09:59 AM

I have a ball bouncing around the screen, and when it hits the walls of the form it changes directions like so.
'Puck move right after hitting left side of wall
:
        If Puck.Left <= ClientRectangle.Left Then
            PuckLeft = True
        End If
        'Puck move left after hitting right side of wall
        If Puck.Left + Puck.Width >= ClientRectangle.Right Then
            PuckLeft = False
        End If
        'Puck move up after hitting bottom side of wall
        If Puck.Top + Puck.Height >= ClientRectangle.Bottom Then
            PuckUp = True
        End If
        'Puck move down after hitting top side of wall
        If Puck.Top <= ClientRectangle.Top Then
            PuckUp = False
        End If


:code:
Now the problem im having is when I use the ________.bounds.intersectswith(_____.bounds).
Only half the player box reacts to the puck. IE: Top, and Right will bounce puck, but the Bottom, and left wont.
Please help me fix this!!!

This post has been edited by modi123_1: 10 June 2015 - 10:01 AM
Reason for edit:: Please use the 'code' button in the editor to apply code tags for readability.

Was This Post Helpful? 0
  • +
  • -

#15 yonkidude   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-June 15

Re: Movement control issues

Posted 10 June 2015 - 10:22 AM

try this

'Puck move up after hitting bottom side of wall
If Puck.Top + Puck.Height >= ClientRectangle.Bottom Then
PuckUp = false
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2