Page 1 of 1
Yet another loop problem. D':
Posted: Mon Mar 07, 2011 1:13 am
by Exiled
I'm having a HUGE loop problem for my map editor. I've been trying to solve it for weeks... I have a ListBox to select which tile you want to edit, and a bunch of buttons determining what the tile will be. It's not the most convenient way to make maps, I know, but I'm new to both map editors and loops so it's the best way I could come up with. Currently when you select different tiles, they progress down diagonally... Not sure why. Also, this only occurs in the (0, 0) through (0, 10) tiles, when the X value changes I get an error.
Here's my code.
Code: Select all
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For si = 0 To 100
If Me.ListBox1.SelectedIndex = si Then
For x = 0 To 10
For y = 0 To 10
sRect = New Rectangle(Map(x, y) * 50, 0, 50, 50)
dRect = New Rectangle(Map(0, x), Map(y, 0), 50, 50)
G.DrawImage(bmp, dRect, sRect, GraphicsUnit.Pixel)
Map(si, si) = 0
Exit For
Next y
Next x
End If
Next si
End Sub
Re: Yet another loop problem. D':
Posted: Mon Mar 07, 2011 7:15 am
by adikid89
visual basic looks disgusting... sorry.. I had to say it...
Re: Yet another loop problem. D':
Posted: Mon Mar 07, 2011 9:13 am
by MrDeathNote
I dont understand why you're calling Exit For in your inner loop. It's not even called conditionally so it executes every time, so that loop has no point.
Re: Yet another loop problem. D':
Posted: Mon Mar 07, 2011 11:18 am
by short
I'm sorry but have you tried stepping through your loop with whateverthefuck debugger vb uses?
Re: Yet another loop problem. D':
Posted: Mon Mar 07, 2011 4:47 pm
by Exiled
@MrDeathNote I guess I put that there because I figured because the loop was being activated by the button I would need to make an Exit For so it wouldn't repeat itself after the button was pressed once. What do you suggest I change?
Re: Yet another loop problem. D':
Posted: Mon Mar 07, 2011 9:20 pm
by dr-snipe
For the Exit for statement, have you tried just y = 11 to make the loop false? I think it may be confusing which loop to exit.
Re: Yet another loop problem. D':
Posted: Tue Mar 08, 2011 2:35 am
by MrDeathNote
I used to program in vb for a very very short time. I'm still not really sure what you're trying to achieve with this code. I assume that this is an event handler, so this code would be executed when the button is clicked and thats it, it wouldn't repeat itself. What you're essentially doing by including that Exit For is this:
Code: Select all
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For si = 0 To 100
If Me.ListBox1.SelectedIndex = si Then
For x = 0 To 10
sRect = New Rectangle(Map(x, 0) * 50, 0, 50, 50)
dRect = New Rectangle(Map(0, x), Map(0, 0), 50, 50)
G.DrawImage(bmp, dRect, sRect, GraphicsUnit.Pixel)
Map(si, si) = 0
Next x
End If
Next si
End Sub
Could you explain a little more about what you're trying to accomplish with this code? Also, what error do you get when the x value changes?
P.S. stepping through the code with the debugger is an excellent suggestion.
Re: Yet another loop problem. D':
Posted: Tue Mar 08, 2011 4:36 am
by Van-B
Also, why are you setting Map(si, si) = 0 ? - that will set a diagonal line from 0,0 to 100,100 on your map. That's some strange code, even for VB - I'm not sure what your trying to do with it.
Re: Yet another loop problem. D':
Posted: Tue Mar 08, 2011 5:43 pm
by Exiled
The (si, si) I was just messing around with the code. It's the closest thing I've had to achieving my goal, so I kept it. Also, the "=0" part is setting the tile to grass. The values are 0 = Grass, 1 = Water, 2 = Sand, 3 = Dirt, etc. etc. etc...
Re: Yet another loop problem. D':
Posted: Wed Mar 09, 2011 8:38 am
by Exiled
@MrDeathNote
I'm trying to make it so when the 'Grass' button is pushed, it will change the selected item in the ListBox to grass. I want the ListBox item to correspond to the correct tile, so I can edit the drawn items while debugging the program. Also, the error I get is 'Value was out of bounds of the array'.
Re: Yet another loop problem. D':
Posted: Wed Mar 09, 2011 9:47 am
by MrDeathNote
Exiled wrote:@MrDeathNote
I'm trying to make it so when the 'Grass' button is pushed, it will change the selected item in the ListBox to grass. I want the ListBox item to correspond to the correct tile, so I can edit the drawn items while debugging the program. Also, the error I get is 'Value was out of bounds of the array'.
So just for claritys sake. The list box holds the coordinate that you want the grass tile to be drawn at when you click the grass button. I just want to make sure i understand you so that i can actually help. If that's the case then there's no need for loops at all, also you would need to hold both an x and a y coord in 2 separate listboxes for this way to work not just loop from 0 to 100 as this is 1 dimensional. Maybe if you posted a screenshot it would be easier to understand.
Re: Yet another loop problem. D':
Posted: Wed Mar 09, 2011 12:16 pm
by Exiled
Alright, here's the screenshot.
- Screenshot.png (30.57 KiB) Viewed 1870 times
Re: Yet another loop problem. D':
Posted: Wed Mar 09, 2011 2:56 pm
by Exiled
WAIT! I GOT IT TO WORK! MrDeathNote, I tried using two listboxes and it worked! I'm not sure whether it'll make me run into problems in the future though...
Here's the code.
Code: Select all
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For si1 = 0 To 64
For si2 = 0 To 64
If Me.ListBox1.SelectedIndex = si1 Then
If Me.ListBox2.SelectedIndex = si2 Then
For x = 0 To 10
For y = 0 To 10
sRect = New Rectangle(Map(x, y) * 50, 0, 50, 50)
dRect = New Rectangle(Map(0, x), Map(y, 0), 50, 50)
G.DrawImage(bmp, dRect, sRect, GraphicsUnit.Pixel)
Map(si1, si2) = 0
Exit For
Next y
Next x
End If
End If
Next si2
Next si1
End Sub
Re: Yet another loop problem. D':
Posted: Thu Mar 10, 2011 2:34 am
by MrDeathNote
Forgive me if i'm a little rusty but wouldn't this do the same thing?
Code: Select all
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For x = 0 To 10
sRect = New Rectangle(Map(x, 0) * 50, 0, 50, 50)
dRect = New Rectangle(Map(0, x), Map(0, 0), 50, 50)
G.DrawImage(bmp, dRect, sRect, GraphicsUnit.Pixel)
Map(Me.ListBox1.SelectedIndex, Me.ListBox2.SelectedIndex) = 0
Next x
End Sub
I could be wrong, I haven't used vb in ages. I'm nearly sure you could make it even more efficient but my vb isn't what it used to be.
Re: Yet another loop problem. D':
Posted: Thu Mar 10, 2011 9:52 am
by Exiled
I actually think that would... I'm not sure if I had to define the Y axis or not.