[2005] No error in code but freezing...
Yes i have been asking for help quite alot but this is just wierd! Take my code below:
vb Code:
Private Sub ItemSpawner_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItemSpawn.Tick
Dim Spawn, X, Y As Integer
Dim Item As PictureBox
Dim Placeable As Boolean
Dim Objects As Control
Placeable = True
Randomize()
Spawn = (Rnd() * 20 + 1)
Background.SendToBack()
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
Do
Item = New PictureBox
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.
Re: [2005] No error in code but freezing...
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
1 Attachment(s)
Re: [2005] No error in code but freezing...
Quote:
Originally Posted by Icyculyr
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.
Re: [2005] No error in code but freezing...
Can you explain a little more about it?
I can't do anything with an exe, and won't launch one.
What exactly does this code do?
Cheers
Re: [2005] No error in code but freezing...
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.
thats basically it!
im going for lunch now, be back in 15.
Re: [2005] No error in code but freezing...
I'd do it like this:
Code:
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...
Cheers
Re: [2005] No error in code but freezing...
wow thanks. It did the trick.
Dam the solution was so easy, just it kept escaping me grasp...!
Thanks again! :D
Re: [2005] No error in code but freezing...
Instead of using Rnd(), you can use Random variable in VB.NET
Code:
Dim rRandom As New Random()
X = rRandom.Next(1, 768) 'I assume Rnd() * 768 + 1 is between 1 & 768
Y = rRandom.Next(1, 560)
Cheers
Re: [2005] No error in code but freezing...
thanks for that again! :P ever so helpful!
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?
Re: [2005] No error in code but freezing...
yes, Case Spawn > 0 AndAlso Spawn < 21 '1-20
Or, you can do this:
Code:
Dim iValidSpawns() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
Code:
Case Array.IndexOf(iValidSpawns, Spawn) <> -1
IndexOf returns -1 if the value is not found, otherwise it contains the index of the item in the array,
So is your problem fixed?
If so read my signature,
Thanks
Re: [2005] No error in code but freezing...
Quote:
Originally Posted by Icyculyr
yes, Case Spawn > 0 AndAlso Spawn < 21 '1-20
Or, you can do this:
Code:
Dim iValidSpawns() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
Code:
Case Array.IndexOf(iValidSpawns, Spawn) <> -1
IndexOf returns -1 if the value is not found, otherwise it contains the index of the item in the array,
So is your problem fixed?
If so read my signature,
Thanks
not quite what i was looking for but it doesnt matter.
thanks!
Re: [2005] No error in code but freezing...
Oh, I see what you mean, something like this:
Code:
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
Cheers