Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

im trying to set up a collision between a picturebox and an array of pictures but I can only get the collision to work with the newest picture in the array. how would I go about getting the collision to work with every image in the array. heres my collision at the moment:

 

If player.Bounds.IntersectsWith(pictureArray(pictureArray.Length - 1).Bounds) Then

End If

 

I think I need to loop it but im having trouble coming up with a way to add this to a loop.

 

 

thanks for any help

  • Leaders
Posted

Mmmmm... for loop...

' Loop through pics
For I As Integer = 0 To PictureArray.Length - 1
   'Test for collision
   If Player.Bounds.InstersectsWith(pictureArray(i).Bounds) Then
       'Do things here. i Is the index of the picture with which we collided.
       MessageBox.Show("Holy Crap! I hit pictureArray(" & i.ToString() & ")")
   End If
Next

[sIGPIC]e[/sIGPIC]
Posted

' Loop through pics
For I As Integer = 0 To PictureArray.Length - 1
   'Test for collision
   If Player.Bounds.InstersectsWith(pictureArray(i).Bounds) Then
       'Do things here. i Is the index of the picture with which we collided.
       MessageBox.Show("Holy Crap! I hit pictureArray(" & i.ToString() & ")")
   End If
Next

 

I forgot to mention that I have the array counting down from 100. so I get the error when I run the program.

 

I tried to change some of the things around but I still get errors when I move my picture (the one im controlling not the one that is in the array).

 

when I do this:

For I As Integer = PictureArray.Length - 1 to 100
   'Test for collision
   If Player.Bounds.InstersectsWith(pictureArray(i).Bounds) Then
       'Do things here. i Is the index of the picture with which we collided.
       MessageBox.Show("Holy Crap! I hit pictureArray(" & i.ToString() & ")")
   End If
Next

 

I don't get an error but the collision doesnt work at all.

 

thanks for your help

Posted
Assuming PictureArray is of length 100 then the code you have provided basically says "for i equals 99 to i equals 100", so it would only detect the collision if you hit object 99 or 100 (which in theory should cause an error if its an array of 100, because 100 would be an out of bounds index). marble_eaters example was perfect and I cannot see why you have got an error with it the only thing I can assume is that there is some confusion somewhere.
Anybody looking for a graduate programmer (Midlands, England)?
Posted (edited)

at the very top I declared my array as a picture box

 

  Dim pictureArray(100) As PictureBox 

 

then in the timer I have the array collision which is the code you gave me

 

         
For I As Integer = 0 To pictureArray.Length - 1
           'Test for collision
           If player.Bounds.IntersectsWith(pictureArray(I).Bounds) Then
               'Do things here. i Is the index of the picture with which we collided.
               MessageBox.Show("Holy Crap! I hit pictureArray(" & I.ToString() & ")")
           End If
       Next

 

in the function that draws the array I have this code:

 

 

ReDim Preserve pictureArray(pictureArray.Length)
       pictureArray(pictureArray.Length - 1) = New PictureBox
'location of picturebox
       pictureArray(pictureArray.Length - 1).Left = 114 + locationleft
       pictureArray(pictureArray.Length - 1).Top = 25 + locationtop
'width and height of picturebox
       pictureArray(pictureArray.Length - 1).Width = 9
       pictureArray(pictureArray.Length - 1).Height = 9
'image for picturebox
       pictureArray(pictureArray.Length - 1).Image = Image.FromFile("ball.jpg")
       Me.Controls.Add(pictureArray(pictureArray.Length - 1))

 

thanks again for your help

Edited by jusanthermemory
Posted

Assumably the code you say is 'function that draws the array' is inside of a loop? From the code you posted, you always seem to change the last object in the array and then add it to your form, since this is the case the other objects in the array are never actually initialised, thus looping through the pictureArray will fail as the other objects don't exist. This means that whilst your form has all 100 PictureBoxes present and correct the pictureArray only contains the last one created.

 

You will need todo something like this when you create the array.

        Dim pictureArray(100) As PictureBox

       ' loop 100 times in order to create 100 objects
       For i As Integer = 0 To 99
           ' create an object that we will later add to the array
           Dim curPicture As New PictureBox
           ' set the objects properties
           curPicture.Left = 114 + locationleft
           curPicture.Top = 25 + locationtop
           curPicture.Width = 9
           curPicture.Height = 9
           curPicture.Image = Image.FromFile("ball.jpg")
           ' add the object to the array
           pictureArray(i) = curPicture
       Next

       ' add the array to the forms control collection so people can see the pictureboxes
       Me.Controls.AddRange(pictureArray)

This will ensure that both the pictureArray and the Controls collection contain 100 references to the same objects. Now anything you do to the pictureArray should be reflected in the Controls collection.

Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...