Yet another loop problem. D':

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Exiled
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Sat Feb 19, 2011 11:34 am
Current Project: Zenoa
Favorite Gaming Platforms: N64, GC, NES
Programming Language of Choice: VB08
Contact:

Yet another loop problem. D':

Post 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
http://www.overlordgaming.tk

"If you can't find something to live for, you best find something to die for."
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: Yet another loop problem. D':

Post by adikid89 »

visual basic looks disgusting... sorry.. I had to say it...
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Yet another loop problem. D':

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Yet another loop problem. D':

Post by short »

I'm sorry but have you tried stepping through your loop with whateverthefuck debugger vb uses?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Exiled
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Sat Feb 19, 2011 11:34 am
Current Project: Zenoa
Favorite Gaming Platforms: N64, GC, NES
Programming Language of Choice: VB08
Contact:

Re: Yet another loop problem. D':

Post 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?
http://www.overlordgaming.tk

"If you can't find something to live for, you best find something to die for."
User avatar
dr-snipe
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Sun Dec 19, 2010 10:09 pm
Programming Language of Choice: C++, Java, PHP
Contact:

Re: Yet another loop problem. D':

Post 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.
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Yet another loop problem. D':

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
Van-B
Chaos Rift Regular
Chaos Rift Regular
Posts: 125
Joined: Tue Aug 10, 2010 7:17 am
Current Project: iPhone puzzle game
Favorite Gaming Platforms: All - Except Amiga
Programming Language of Choice: DBPro, ObjC++
Location: Scotland

Re: Yet another loop problem. D':

Post 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.
Health, ammo.... and bacon and eggs.
User avatar
Exiled
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Sat Feb 19, 2011 11:34 am
Current Project: Zenoa
Favorite Gaming Platforms: N64, GC, NES
Programming Language of Choice: VB08
Contact:

Re: Yet another loop problem. D':

Post 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...
http://www.overlordgaming.tk

"If you can't find something to live for, you best find something to die for."
User avatar
Exiled
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Sat Feb 19, 2011 11:34 am
Current Project: Zenoa
Favorite Gaming Platforms: N64, GC, NES
Programming Language of Choice: VB08
Contact:

Re: Yet another loop problem. D':

Post 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'.
http://www.overlordgaming.tk

"If you can't find something to live for, you best find something to die for."
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Yet another loop problem. D':

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
Exiled
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Sat Feb 19, 2011 11:34 am
Current Project: Zenoa
Favorite Gaming Platforms: N64, GC, NES
Programming Language of Choice: VB08
Contact:

Re: Yet another loop problem. D':

Post by Exiled »

Alright, here's the screenshot.
Screenshot.png
Screenshot.png (30.57 KiB) Viewed 1867 times
http://www.overlordgaming.tk

"If you can't find something to live for, you best find something to die for."
User avatar
Exiled
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Sat Feb 19, 2011 11:34 am
Current Project: Zenoa
Favorite Gaming Platforms: N64, GC, NES
Programming Language of Choice: VB08
Contact:

Re: Yet another loop problem. D':

Post 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
http://www.overlordgaming.tk

"If you can't find something to live for, you best find something to die for."
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Yet another loop problem. D':

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
Exiled
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Sat Feb 19, 2011 11:34 am
Current Project: Zenoa
Favorite Gaming Platforms: N64, GC, NES
Programming Language of Choice: VB08
Contact:

Re: Yet another loop problem. D':

Post by Exiled »

I actually think that would... I'm not sure if I had to define the Y axis or not.
http://www.overlordgaming.tk

"If you can't find something to live for, you best find something to die for."
Post Reply