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.
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
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.
@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?
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:
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.
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.
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...
@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'.
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.
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.
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
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.