Results 1 to 12 of 12

Thread: [2005] No error in code but freezing...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    95

    Resolved [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:
    1. Private Sub ItemSpawner_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItemSpawn.Tick
    2.         Dim Spawn, X, Y As Integer
    3.         Dim Item As PictureBox
    4.         Dim Placeable As Boolean
    5.         Dim Objects As Control
    6.         Placeable = True
    7.         Randomize()
    8.         Spawn = (Rnd() * 20 + 1)
    9.         Background.SendToBack()
    10.         Select Case Spawn
    11.             Case Is = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    12.                 'FireBall
    13.                 Do
    14.                     Item = New PictureBox
    15.                     Item.Image = Image.FromFile("FireBall.gif")   '<<<--- Also Freezes at this point sometimes.
    16.                     Item.Width = 26
    17.                     Item.Height = 30
    18.                     Controls.Add(Item)
    19.                     X = (Rnd() * 760 + 1)
    20.                     Y = (Rnd() * 560 + 1)
    21.                     Item.Location = New Point(X, Y)   '<<<--- Freezes at this point on repeat!
    22.                     Item.Tag = "Item Fireball"
    23.                     For Each Objects In Controls
    24.                         If TypeOf Objects Is PictureBox AndAlso Objects.Tag = "wall" Then
    25.                             If Objects.Bounds.IntersectsWith(Item.Bounds) Then
    26.                                 Placeable = False
    27.                             End If
    28.                         End If
    29.                     Next Objects
    30.                 Loop Until Placeable = True
    31.                 Item.Visible = True
    32.         End Select
    33.     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.

  2. #2
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    95

    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:
    1. Do
    2.                     Placeable = True
    3.                     Item = New PictureBox
    4.                     Item.Image = Image.FromFile("FireBall.gif")
    5.                     Item.Width = 26
    6.                     Item.Height = 30
    7.                     Controls.Add(Item)
    8.                     X = (Rnd() * 760 + 1)
    9.                     Y = (Rnd() * 560 + 1)
    10.                     Item.Location = New Point(X, Y)
    11.                     Item.Tag = "Item Fireball"
    12.                     For Each Objects In Controls
    13.                         If TypeOf Objects Is PictureBox AndAlso Objects.Tag = "wall" Then
    14.                             If Objects.Bounds.IntersectsWith(Item.Bounds) Then
    15.                                 Placeable = False
    16.                             End If
    17.                         End If
    18.                     Next Objects
    19.                 Loop Until Placeable = True
    20.                 Item.Visible = True

    press q if you want to get rid of annoying fireballs off the screen.
    Attached Files Attached Files

  4. #4
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    95

    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.

  6. #6
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    95

    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!

  8. #8
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    95

    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?

  10. #10
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    95

    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!

  12. #12
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width