如何检测与衍生对象的碰撞?

时间:2014-04-22 14:13:02

标签: vb.net

Public Class Form1

    Dim i As Integer    'integer for spawning
    Dim maxball As Integer = 50 'max ball able to be created
    Dim RateX(maxball) As Integer   'rate of movement
    Dim RateY(maxball) As Integer   'rate of movement
    Dim ball(maxball) As PictureBox 'spawned ball is a picture box
    Dim rnd As New Random   'random number generator
    Dim rndLoc As Integer   'random locatino generator
    Dim Loc As Point    'location is a point on the screen
    Dim create As Integer   'integer to create new balls
    Dim score As Integer = 0    'score is 0 but can increase

    'move the ball
    Private Sub moveball()
        'For Each ball(ec) In ball
        For i As Integer = 0 To create - 1
            If ball(i).Left <= pbArena.Left Then   'bounce off left side
                RateX(i) *= -1
            End If
            If ball(i).Right >= pbArena.Right Then   'bounce off right side
                RateX(i) *= -1
            End If
            If ball(i).Top <= pbArena.Top Then    'bounce off top
                RateY(i) *= -1
            End If
            If ball(i).Bottom >= pbArena.Bottom Then  'bounce off bottom
                RateY(i) *= -1
            End If
            '====================================================================================================================================================
            ball(i).Left += RateX(i)  'moves the ball horizontally
            ball(i).Top += RateY(i) 'moves the ball vertically
            '====================================================================================================================================================
        Next
    End Sub
    'create the ball
    Private Sub createball()
        If create <= 50 Then    '50 is max amount to
            create += 1 'add 1 to create
            ball(i) = New PictureBox    'ball is a picture box
            ball(i).Size = New Size(45, 45) 'set size
            ball(i).BackColor = Color.Red   'set color
            '====================================================================================================================================================
            ball(i).Top = rnd.Next(pbArena.Height - ball(i).Height)    'sets random y
            ball(i).Left = rnd.Next(pbArena.Width - ball(i).Width)     'sets random x
            '====================================================================================================================================================
            RateX(i) = rnd.Next(-4, 4)   'random X direction/speed
            RateY(i) = rnd.Next(-4, 4)   'random Y direction/speed
            '====================================================================================================================================================
            Me.Controls.Add(ball(i))    'actually add teh ball
            ball(i).BringToFront()  'bring to front so arena isn't in front
            i += 1
        End If
    End Sub
    'commands for when you touch black box
    Private Sub pbTarget_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbTarget.MouseEnter
        pbTarget.Top = rnd.Next(pbArena.Height - pbTarget.Height)    'sets random y
        pbTarget.Left = rnd.Next(pbArena.Width - pbTarget.Width)     'sets random x
        '====================================================================================================================================================
        'scoring system
        score = score + 1
        lblScore.Text = score
        createball()    'creates a new ball
    End Sub
    'what happens when the timer ticks
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        moveball()  'every timer tick the ball will move IF its created
        ball(i) = New PictureBox    'ball is a picture box
    End Sub
End Class

到目前为止,这是我的代码。每次鼠标与目标(图片框)相交时都会移动。我正在复制这个游戏。 http://www.lewpen.com/game/我使用了一个数组在表单上生成红色方块。我希望能够检测到鼠标何时进入它们。我知道如何用图片框来做这件事,但这些都是产生的对象叫做ball(i)。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

听起来您想知道如何将事件处理程序添加到动态创建的控件中。 these are all spawned objects called ball(i)但它们都只是图片盒。您可以在创建球(图片框)时添加事件处理程序

Private Sub createball(BallIndex As Integer)
   Dim ball As New PictureBox    'ball is a picture box
   ' give it a name so we can find it
   ' ball index is PASSED to avoid sloppy global vars
   ball.Name = "Ball" & BallIndex.ToString 
   ' etc
   Me.Controls.Add(ball)
   AddHandler ball.MouseEnter, AddressOf BallMouseEnter

在其他地方,您需要添加事件代码:

Private Sub BallMouseEnter(sender As Object, e As EventArgs) 
    ' your code here
End Sub

由于球作为Controls集合中的控件存在,因此没有理由在数组中保留对它们的引用。如果您将它们命名为“Ball1”,“Ball2”,您可以通过名称引用它们来移动它们:

Me.Controls(ballname)

BallName将是"Ball" & index.ToString,而index将是要移动的球/图片框(如i变量)。 编辑更多信息:

Private Sub moveballS()
    Dim ball As PictureBox

    ' loop thru ballS
    For n As Integer = 0 To BallCount
        ' current ball from name
        ball = Me.Controls("Ball" & n.ToString)

        ' your code here
        If ball.Left <= pbArena.Left Then 
        ' etc
        End If

        ' you CAN just reference them in controls(),
        ' but it is wordy:
        If Controls("Ball" & n.ToString).Left <= pbArena.Left Then 
            ' etc
        End If

     Next n
End Sub

跟踪它们的另一种方法只是存储名称的List(Of String),但如果您可以通过上面的Controls()按名称获取它们,则同样不需要它:

Dim ballList As New List(Of String)

' when you create a ball:
ballList.Add(ball.Name)   

获取控件参考:

For n As Integer = 0 To ballList.Count - 1
   ball = Me.Controls(ballList(n))
   ' etc

创建对动态创建的控件(如数组)的单独引用可能是一个坏主意,因为如果/当您重置或重新开始并且正在删除球/图片框时,该引用可以防止对象被丢弃。 / p>