VB:检查对象是否具有“类”然后执行代码

时间:2016-08-26 11:19:29

标签: vb.net vba visual-studio

我希望在Visual Basic中找到一些基本上说明如果某些东西与某个类碰撞的东西,它会执行代码。我可能没有使用正确的术语,所以当我说对象时我的意思就像一个标签或图片框,一个类......就像一个HTML类,我想,就像一个具有特征的对象。

伪代码看起来像这样:

If object.Bounds.IntersectsWith([object with certain class]) Then execute code

否则,游戏'如果'语句将不堪重负...我也是Visual Basic语言的新手,所以请尽量保持简单。

这是我已经拥有的:

    If carblue.Bounds.IntersectsWith(boundary1.Bounds) And directiona = 1 Then
        directiona = 0
        carblue.Top += 2
    ElseIf carblue.Bounds.IntersectsWith(boundary1.Bounds) And directiona = 2 Then
        directiona = 0
        carblue.Left += 2
    ElseIf carblue.Bounds.IntersectsWith(boundary1.Bounds) And directiona = 3 Then
        directiona = 0
        carblue.Top -= 2
    ElseIf carblue.Bounds.IntersectsWith(boundary1.Bounds) And directiona = 4 Then
        directiona = 0
        carblue.Left -= 2
    End If

如果汽油是被控制的物体,则border1是阻止汽车移动(碰撞时)的障碍物,而directiona是汽车行驶方向的值(1为向上,2为剩余等)。< / p>

(移动自S.A。程序员网站)

2 个答案:

答案 0 :(得分:0)

如果没有实际的示例代码,提供特定的解决方案并不容易。我不知道你有多少控件移动,是2吗? 10?万?假设你有10个以上的控件移动,这就是我将如何处理它。

我会使用数据表来记录每个移动或可碰撞的控件的边界。控件移动后,更新数据表中的该行,查找冲突,然后检查对象类型以确定冲突对象的类类型,然后根据需要运行代码。

    Public MovingControls As DataTable

    Sub Main()
        'Build the DataTable
        MovingControls = New DataTable("ControlBounds")
        MovingControls.Columns.Add("Name", GetType(String))
        MovingControls.Columns.Add("x1", GetType(Integer))
        MovingControls.Columns.Add("x2", GetType(Integer))
        MovingControls.Columns.Add("y1", GetType(Integer))
        MovingControls.Columns.Add("y2", GetType(Integer))
    End Sub

    'Call this only when a control/object is created
    Sub MovingControlAdded(sender As Control)
        Dim Row As DataRow = MovingControls.NewRow
        Row("Name") = sender.Name
        Dim BoundsRect As Drawing.Rectangle = sender.Bounds
        Row("x1") = sender.Bounds.Left
        Row("x2") = sender.Bounds.Right
        Row("y1") = sender.Bounds.Bottom
        Row("y2") = sender.Bounds.Top
        MovingControls.Rows.Add(Row)
    End Sub

    'Call this only when a control/object has moved
    Sub MovingControlMoved(sender As Control)
        'Update the location of this Control
        Dim Row() As DataRow = MovingControls.Select("Name = '" & sender.Name & "'")
        'Select returns an array of Rows but there should only be 1 row for each Control
        Row(0)("x1") = sender.Bounds.Left
        Row(0)("x2") = sender.Bounds.Right
        Row(0)("y1") = sender.Bounds.Bottom
        Row(0)("y2") = sender.Bounds.Top
        'Collision check
        Dim CollidedRows() As DataRow = MovingControls.Select("(" & sender.Bounds.Right & " >= x1)" &
                                                              "AND (" & sender.Bounds.Left & " <= x2)" &
                                                              "AND (" & sender.Bounds.Bottom & " <= y2)" &
                                                              "AND (" & sender.Bounds.Top & " >= y1)" &
                                                              "AND (Name <> '" & sender.Name & "'")
        'Determine the object type and execute necessary code
        For Each CollidedRow As DataRow In CollidedRows
            Dim CollidedControl As Control = Me.Controls.Item(CollidedRow("Name"))
            If CollidedControl.GetType = GetType(Label) Then
                'Do stuff for labels
            ElseIf CollidedControl.GetType = GetType(Button) Then
                'Do stuff for buttons
            End If
        Next
    End Sub

警告:这假设一次控制一个控制。如果控制A移动到控制B但控制B同时移开,则即使避免了碰撞,该代码仍会调用碰撞。如果有多个控件移动,您可能希望将MovingControlMoved方法拆分为2个方法,一个用于更新表,另一个用于碰撞检查。首先处理所有运动,然后处理所有碰撞。

根据复杂程度,您可能希望为可继承控件的自定义类创建自定义类,以继承碰撞接口。您可以使用System.Reflection来调用RunMeOnCollision。这将消除If语句列表。

Interface iCollidableBase
    Sub RunMeOnCollision()
End Interface

Public Class CollidableLabel
    Inherits Label
    Implements iCollidableBase

    Public Sub RunMeOnCollision() Implements iCollidableBase.RunMeOnCollision
        Me.Text = "I have been collided"
    End Sub

End Class

Public Class CollidableButton
    Inherits Button
    Implements iCollidableBase

    Public Sub RunMeOnCollision() Implements iCollidableBase.RunMeOnCollision
        Me.Text = "Ouch, that collision hurt!"
    End Sub

End Class

同样,我不知道这里的完整上下文,我无法针对您的代码测试我的解决方案。如果您可以发布一些其他详细信息,我可以提供更多帮助。

-E

答案 1 :(得分:0)

我使用数组从编程老师那里找到了我需要的东西。

在公共子: Dim walls(17) As PictureBox 其中17是表单所具有的(在本例中)边界的数量。同时将PictureBox更改为您正在使用的对象(例如标签)。 然后在表单中加载:

 For i = 1 To 17
        walls(i) = Me.Controls("boundary" & i)
    Next

老实说,我不是百分之百确定这一点,而是&#34;界限&#34; string是my PictureBoxes名称的一部分,用作边界。名称是boundary1,boundary2等。所以你可以改变&#34; boundary&#34;你对象的名字是什么。

接下来,当您想使用边界检查某些内容时,请声明

 For i = 1 To 17

然后当你想要关闭它时,

Next

要检查碰撞,请使用:

If object.Bounds.IntersectsWith(walls(i).Bounds) Then

因此,在这种情况下,If语句将介于For和Next行之间。

相关问题