为什么我的球能够在上升时左右移动而在向左移动时不能向下移动?

时间:2015-09-05 02:08:37

标签: vb.net visual-studio visual-studio-2010

我对visual basic相当新,我想开始学习如何使用构造函数并使用自己的hitbox声明矩形形状。我遇到的问题是当控制球的人在触摸盒子底部时同时点击向上箭头和向左/向右箭头时,它会左右移动,但是当我使用时左箭头和向下箭头,同时触摸盒子的右侧,它不会下降(它确实上升)。我也在使用 Visual Studio 2010。

盒子的某些部分就是这种情况,有时你可以按住2个箭头键并且球会正常移动但是其他时候球会停止:这是我的代码,如果有人可以提供帮助那就太棒了!

我的构造函数:

Public Class Box
Inherits Microsoft.VisualBasic.PowerPacks.RectangleShape
Sub position(ByVal xPos As Double, ByVal yPos As Double)
    Left = xPos
    Top = yPos
End Sub

Sub dimension(ByVal h As Double, ByVal w As Double)
    Height = h
    Width = w
End Sub
Sub hitbox()
    If (Form1.Ball1.Location.Y + Form1.Ball1.Height > Top) And (Form1.Ball1.Location.Y < Bottom) And (Form1.Ball1.Location.X + Form1.Ball1.Width > Left) And (Form1.Ball1.Location.X < Right) And (Form1.globalVariables.velocityW = True) Then
        Form1.Ball1.Location = New Point(Form1.Ball1.Location.X, Form1.Ball1.Location.Y + Form1.globalVariables.posInc)
    End If

    If (Form1.Ball1.Location.Y + Form1.Ball1.Height > Top) And (Form1.Ball1.Location.Y < Bottom) And (Form1.Ball1.Location.X + Form1.Ball1.Width > Left) And (Form1.Ball1.Location.X < Right) And (Form1.globalVariables.velocityA = True) Then
        Form1.Ball1.Location = New Point(Form1.Ball1.Location.X + Form1.globalVariables.posInc, Form1.Ball1.Location.Y)
    End If

    If (Form1.Ball1.Location.Y + Form1.Ball1.Height > Top) And (Form1.Ball1.Location.Y < Bottom) And (Form1.Ball1.Location.X + Form1.Ball1.Width > Left) And (Form1.Ball1.Location.X < Right) And (Form1.globalVariables.velocityS = True) Then
        Form1.Ball1.Location = New Point(Form1.Ball1.Location.X, Form1.Ball1.Location.Y - Form1.globalVariables.posInc)
    End If

    If (Form1.Ball1.Location.Y + Form1.Ball1.Height > Top) And (Form1.Ball1.Location.Y < Bottom) And (Form1.Ball1.Location.X + Form1.Ball1.Width > Left) And (Form1.Ball1.Location.X < Right) And (Form1.globalVariables.velocityD = True) Then
        Form1.Ball1.Location = New Point(Form1.Ball1.Location.X - Form1.globalVariables.posInc, Form1.Ball1.Location.Y)
    End If
End Sub
End Class

这是我的表格(form1):

Imports Constructor.Box
Public Class Form1
Dim box1 As New Box
Dim box2 As New Box
Public Class globalVariables
    Public Shared velocityW As Boolean = False
    Public Shared velocityA As Boolean = False
    Public Shared velocityS As Boolean = False
    Public Shared velocityD As Boolean = False
    Public Shared posInc As Double = 5
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

Public Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    tmrAll.Start()
    Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
    canvas.Parent = Me
    Ball1.Location = New Point(300, 300)

    box1.dimension(300, 50)
    box1.position(100, 100)
    box1.Parent = canvas


    box2.dimension(50, 300)
    box2.position(100, 50)
    box2.Parent = canvas
End Sub
Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    If e.KeyData = Keys.D Then
        globalVariables.velocityD = True
    End If

    If e.KeyData = Keys.A Then
        globalVariables.velocityA = True
    End If

    If e.KeyData = Keys.W Then
        globalVariables.velocityW = True
    End If

    If e.KeyData = Keys.S Then
        globalVariables.velocityS = True
    End If
End Sub
Private Sub frmMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

    If e.KeyData = Keys.D Then
        globalVariables.velocityD = False
    End If

    If e.KeyData = Keys.A Then
        globalVariables.velocityA = False
    End If

    If e.KeyData = Keys.W Then
        globalVariables.velocityW = False
    End If

    If e.KeyData = Keys.S Then
        globalVariables.velocityS = False
    End If
End Sub
Private Sub tmrAll_Tick(sender As System.Object, e As System.EventArgs) Handles tmrAll.Tick

    If globalVariables.velocityW = True Then
        Ball1.Top -= globalVariables.posInc
    End If
    If globalVariables.velocityA = True Then
        Ball1.Left -= globalVariables.posInc
    End If
    If globalVariables.velocityS = True Then
        Ball1.Top += globalVariables.posInc
    End If
    If globalVariables.velocityD = True Then
        Ball1.Left += globalVariables.posInc
    End If


    box1.hitbox()
    box1.Refresh()

    box2.hitbox()
    box2.Refresh()



    If Ball1.Location.X + Ball1.Width + Ball1.Width / 4.1 > Me.Width Then
        Ball1.Location = New Point(Ball1.Location.X - globalVariables.posInc, Ball1.Location.Y)
    End If

    If Ball1.Location.X < 0 Then
        Ball1.Location = New Point(Ball1.Location.X + globalVariables.posInc, Ball1.Location.Y)
    End If


    If Ball1.Location.Y + Ball1.Width + Ball1.Width / 1.3 > Me.Height Then
        Ball1.Location = New Point(Ball1.Location.X, Ball1.Location.Y - globalVariables.posInc)
    End If

    If Ball1.Location.Y < 0 Then
        Ball1.Location = New Point(Ball1.Location.X, Ball1.Location.Y + globalVariables.posInc)
    End If
End Sub
End Class

再次感谢能够花时间查看我的代码的任何人。我也明白代码可能效率不高。

0 个答案:

没有答案