How do I detect collision with spawned objects?

Canucksctr7
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

This is my code so far. Each time the mouse intersects with the target (which is a picture box) is moves. I am replicating this game. http://www.lewpen.com/game/ I have used an array to spawn red squares on the form. I want to be able to detect when my mouse enters them. I know how to do this with picture boxes, but these are all spawned objects called ball(i). Thanks for the help!

Ňɏssa Pøngjǣrdenlarp

It sounds like you want to know how to add an event handler to a dynamically created control. these are all spawned objects called ball(i) but they are all just pictureboxes. You can add event handlers when you create the balls (pictureboxes)

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

Elsewhere, you'd add the code for the event:

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

Since the balls exist as controls in the Controls collection there is really no reason to keep a reference to them in an array. If you name them "Ball1", "Ball2" you can make them move by referencing them by name:

Me.Controls(ballname)

Where BallName would be "Ball" & index.ToString and index would be the ball/picturebox to move (like the i variable). EDIT More info:

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

Another way to track them is just a List(Of String) to store the name, but that is equally unneeded if you can get them by name from Controls() as above:

Dim ballList As New List(Of String)

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

to get a control reference:

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

It can be a bad idea to create a separate reference to dynamically created controls (like an array) since that ref can prevent the object from being disposed of if/when you reset or start over and are deleting balls/pictureboxes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to detect a collision in sprite kit?

From Dev

how do I sent escape character ^] through a spawned telnet session?

From Dev

How can I detect when the jQuery UI collision fliped

From Dev

Detect the collision point of two objects

From Dev

How do I detect collision with spawned objects?

From Dev

How may i detect contact and collision correctly? Swift iOS SKScene

From Dev

Detect collision between two objects in Swift

From Dev

How to detect collision between two objects in JavaScript with Three.js?

From Dev

How do I detect the collision of two sprites using pygame?

From Dev

How to detect shape collision - Android

From Dev

How do I ensure that a spawned Child process is killed if my app panics?

From Dev

How do I get the pid of a spawned process using pexpect?

From Dev

How do I preserve colors in the console when running a script from a spawned child process?

From Dev

How to detect if a Node spawned process is still running?

From Dev

How do I limit the number of concurrent processes spawned by Proc::Async in Perl 6?

From Dev

How to detect a collision in sprite kit?

From Dev

How do I detect collision with C++, OpenGL, and freeglut?

From Dev

Detect the collision point of two objects

From Dev

Java detect collision for fast moving objects

From Dev

How may i detect contact and collision correctly? Swift iOS SKScene

From Dev

How do I detect the collision of two sprites using pygame?

From Dev

How do I detect overlapping almost circular objects in MATLAB?

From Dev

How do I adding to score a collision?

From Dev

How to detect collision without physics

From Dev

How do I get the pid of a spawned process using pexpect?

From Dev

How do i detect collision between 2 rectangles??? LIBGDX

From Dev

how do I write to a spawned terminal?

From Dev

how do i set collision detection to where i want it?

From Dev

Detect Collision between two objects and make points increase on collision

Related Related

  1. 1

    How to detect a collision in sprite kit?

  2. 2

    how do I sent escape character ^] through a spawned telnet session?

  3. 3

    How can I detect when the jQuery UI collision fliped

  4. 4

    Detect the collision point of two objects

  5. 5

    How do I detect collision with spawned objects?

  6. 6

    How may i detect contact and collision correctly? Swift iOS SKScene

  7. 7

    Detect collision between two objects in Swift

  8. 8

    How to detect collision between two objects in JavaScript with Three.js?

  9. 9

    How do I detect the collision of two sprites using pygame?

  10. 10

    How to detect shape collision - Android

  11. 11

    How do I ensure that a spawned Child process is killed if my app panics?

  12. 12

    How do I get the pid of a spawned process using pexpect?

  13. 13

    How do I preserve colors in the console when running a script from a spawned child process?

  14. 14

    How to detect if a Node spawned process is still running?

  15. 15

    How do I limit the number of concurrent processes spawned by Proc::Async in Perl 6?

  16. 16

    How to detect a collision in sprite kit?

  17. 17

    How do I detect collision with C++, OpenGL, and freeglut?

  18. 18

    Detect the collision point of two objects

  19. 19

    Java detect collision for fast moving objects

  20. 20

    How may i detect contact and collision correctly? Swift iOS SKScene

  21. 21

    How do I detect the collision of two sprites using pygame?

  22. 22

    How do I detect overlapping almost circular objects in MATLAB?

  23. 23

    How do I adding to score a collision?

  24. 24

    How to detect collision without physics

  25. 25

    How do I get the pid of a spawned process using pexpect?

  26. 26

    How do i detect collision between 2 rectangles??? LIBGDX

  27. 27

    how do I write to a spawned terminal?

  28. 28

    how do i set collision detection to where i want it?

  29. 29

    Detect Collision between two objects and make points increase on collision

HotTag

Archive