Item.Image = Image.FromFile("FireBall.gif") '<<<--- Also Freezes at this point sometimes.
Item.Width = 26
Item.Height = 30
Controls.Add(Item)
X = (Rnd() * 760 + 1)
Y = (Rnd() * 560 + 1)
Item.Location = New Point(X, Y) '<<<--- Freezes at this point on repeat!
Item.Tag = "Item Fireball"
For Each Objects In Controls
If TypeOf Objects Is PictureBox AndAlso Objects.Tag = "wall" Then
If Objects.Bounds.IntersectsWith(Item.Bounds) Then
Placeable = False
End If
End If
Next Objects
Loop Until Placeable = True
Item.Visible = True
End Select
End Sub
It runs the code 1st time well but on the loop it freezes. So it'll place the object fine and it it does not collide with anything, the code passes on and all i well. However if the object the placed and it collides, i tell it to reposition be looping the code again. This causes problems... the program just Freezes! No error, just freeze!
I just cant see what is causing this. To me, the code although not Perfect, should work.
can anyone see what is wrong with it?
If requested, ill pm source files since this is my groups assignment.
Last edited by squrrilslayer; Apr 3rd, 2008 at 09:11 PM.
See what your doing, is setting placeable to true outside the loop, and setting it to false inside the loop, now I think you need to set it to true, at the start of the DO loop, so each time Objects Is a picturebox, it's tag is Wall, and it intersects with item.bounds, it won't stay false the next time.
See what your doing, is setting placeable to true outside the loop, and setting it to false inside the loop, now I think you need to set it to true, at the start of the DO loop, so each time Objects Is a picturebox, it's tag is Wall, and it intersects with item.bounds, it won't stay false the next time.
Cheers
hehe... now that i've done that, take a look :P (at the .exe that is)
being swamped by spawns!
somehow i think that setting it to true inside the loop is overriding the false at the bottom...
vb Code:
Do
Placeable = True
Item = New PictureBox
Item.Image = Image.FromFile("FireBall.gif")
Item.Width = 26
Item.Height = 30
Controls.Add(Item)
X = (Rnd() * 760 + 1)
Y = (Rnd() * 560 + 1)
Item.Location = New Point(X, Y)
Item.Tag = "Item Fireball"
For Each Objects In Controls
If TypeOf Objects Is PictureBox AndAlso Objects.Tag = "wall" Then
If Objects.Bounds.IntersectsWith(Item.Bounds) Then
Placeable = False
End If
End If
Next Objects
Loop Until Placeable = True
Item.Visible = True
press q if you want to get rid of annoying fireballs off the screen.
i understand you not wanting to open the .exe its there if you want it though.
Basically i have the main form, which is a maze/duel arena. I want random items to spawn every X seconds around the map. It however looks dodgy when an item spawns in a wall or on the UI therefore im trying to make it so if the spawned item has collided with a wall (spawned ontop of one), relocate it.
Select Case Spawn
Case Is = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
'FireBall
Item = New PictureBox()
Item.Image = Image.FromFile("FireBall.gif")
Item.Width = 26
Item.Height = 30
Controls.Add(Item)
Do
Placeable = true
X = (Rnd() * 760 + 1)
Y = (Rnd() * 560 + 1)
Item.Location = New Point(X, Y) '<<<--- Freezes at this point on repeat!
Item.Tag = "Item Fireball"
For Each oObject As Object In Controls
If TypeOf oObject Is PictureBox AndAlso oObject .Tag = "wall" Then
If oObject .Bounds.IntersectsWith(Item.Bounds) Then
Placeable = False
End If
End If
Next Objects
Loop Until Placeable = True
Item.Visible = True
End Select
Try that, and see how it goes, I have a few more idea's as well,
I believe the problem is because your loading the fireball image repeatedly, and adding controls and controls until Placeable is true, but Placeable will never be true if the first position of the fireball hit's a wall, so you loop forever!
and I recommend you have a better 'naming system'
I prefer to use oObject, pbItem, bPlaceable etc... it is more organized...
I just realised though see how my select case i have it going all the way up to 20... that is 20 different spells/items... is there a neater way to do the above without copy+paste the code 20 times?
Dim sSpellNames() As String = {"FireBall"}
Dim sSpellIPaths() As String = {"PathToFireBall"}
For i As Integer = 0 To sSpellNames.Length-1
'Replace any normal strings like the Image.FromFile("Fireball.gif") path etc..
'With the correct, IE sSpellIPaths would be the parameter for Image.FromFile, and sSpellNames would be the tag.
Next i