visual basic stuff

Discussion in 'Scripting & Programming' started by zxspectrum, Apr 4, 2007.

  1. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    Thats your problem, you are setting the gollum text to the frog text.

    Also, using timer1.enabled isnt really the best option, but thats not how you are going to be using the timer. With the timer in my example, once the tick hits it will fire off the specified example, in which case you need to evaluate the position of the gollum in relation to its current target. You dont need to evaluate if the timer is running or not, as the sub doesnt fire without it (unless you manually fire it elsewhere, but even then you dont need to evaluate if its running).

    Also, comparing the texts isnt a valid choice. The text is how it appears on the screen in this case, you dont want to manipulate it at all, and you certainly cant use it to determine if the gollum has caught the frog, you want to use the co-ordinate methods available in vb.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  2. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,972
    161
    189
    Right Fergal ive been messing with the code a bit on the message box and i can get the gollum to keep its own letter, hurrah.

    I still have things to tidy up on it but my heads wrecking so ill move onto something else. The code he has for moving the frogs is my next target.

    The prog i sent you has all this code for frog1
    If frogselected = "1" Then
    If dirselected = "U" Then
    lblF1.Top = lblF1.Top - 5
    End If
    If dirselected = "UR" Then
    lblF1.Top = lblF1.Top - 5
    lblF1.Left = lblF1.Left + 5
    End If
    If dirselected = "R" Then
    lblF1.Left = lblF1.Left + 5
    End If
    If dirselected = "DR" Then
    lblF1.Top = lblF1.Top + 5
    lblF1.Left = lblF1.Left + 5
    End If
    If dirselected = "D" Then
    lblF1.Top = lblF1.Top + 5
    End If
    If dirselected = "DL" Then
    lblF1.Top = lblF1.Top + 5
    lblF1.Left = lblF1.Left - 5
    End If
    If dirselected = "L" Then
    lblF1.Left = lblF1.Left - 5
    End If
    If dirselected = "UL" Then
    lblF1.Top = lblF1.Top - 5
    lblF1.Left = lblF1.Left - 5
    End If
    End If

    Now that to me seems somewhat too long even though it does work. What would you reccommend here? multi dimensional arrays or case select statements???

    Ive sort of started in the way of case select heres what ive ome up with so far

    Timer1.Enabled = True
    Select Case frogselected
    Case "1", "2", "3"
    End Select
    Select Case dirselected
    Case "U", "D", "L", "R", "UR", "UL", "DR", "DL"
    Case "U" = "1"
    lblF1.Top = lblF1.Top - 5
    Case "UR" = "1"
    lblF1.Top = lblF1.Top - 5
    lblF1.Left = lblF1.Left + 5
    Case "R" = "1"
    lblF1.Left = lblF1.Left + 5
    Case "DR" = "1"
    lblF1.Top = lblF1.Top + 5
    lblF1.Left = lblF1.Left + 5
    End Select

    It goes without saying this doesnt yet work lol

    Eddie
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  3. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    well, if this is the code for determining the direction the specific frog moves in, then i would seriously suggest trying to cut the code down.

    Im assuming this code is exactly the same for every frog, written separately, so id try to figure out an easy way to do this.

    The entire function in closer is appalling to be honest, its a monster. For a start they are evaluating the same stuff again. When theres no point. Personally i would do something like this:

    Code:
       Private Sub SetDirection(ByVal frog As Label)
            Select Case dirselected
                Case "U"
                    frog.Top = frog.Top - 5
                Case "UR"
                    frog.Top = frog.Top - 5
                    frog.Left = frog.Left + 5
                Case "R"
                    frog.Left = frog.Left - 5
                Case "DR"
                    frog.Top = frog.Top + 5
                    frog.Left = frog.Left + 5
                Case "D"
                    frog.Top = frog.Top + 5
                Case "DL"
                    frog.Top = frog.Top + 5
                    frog.Left = frog.Left - 5
                Case "L"
                    frog.Left = frog.Left - 5
                Case "UL"
                    frog.Top = frog.Top - 5
                    frog.Left = frog.Left - 5
            End Select
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Select Case frogselected
                Case "1"
                    SetDirection(lblF1)
                Case "2"
                    SetDirection(lblF2)
                Case "3"
                    SetDirection(lblF3)
                Case Else
                    'do nothing
            End Select
    Do you see how i moved the entire direction change out of the sub and into one of its own? I did this so im not having to rewrite the code for every single frog. Since its the same for each of them, there is no point rewriting it for every one of them. You can then do the same for the next section about their positions, and incorporate the calls into the initial select case statement in the timer tick, further reducing the size of the Sub.

    Ideally, any given sub/function will be at most 1-1.5 screens i believe. you dont want a sub where you have to scroll through screens and screens of data. If you have something like that, then its poorly coded and you need to rethink how you are achieving things. With the example of this particular sub, whoever wrote it is making the same calculations several times over, for each individual frog, when it could easily be reduced to a function that doesnt actually care WHAT frog its calculating for.

    Oh, see when you reference the code, can you please use the code tags (like quote but the word code instead) as it looks neater, and please reference the function name - it makes it easier for me to see it in the code).

    EDIT: incase you think ive missed something, i deliberately havent closed the timer1_tick sub since there is more code beyond what ive shown.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  4. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,972
    161
    189
    Cheers for all that fergal , this is helping me out a lot

    When you mention code tags, do you mean annotation??

    And referncing the the function name ??? im not too sure on that

    Thanks a ton tho
    Eddie
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  5. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    see in my code where i show the function name? ie

    Code:
    Private Sub SetDirection(ByVal frog As Label)
    Thats the function name, if you include it, i can see where in the code you supplied the code is placed.

    And i mean the code tags like that just appeared to encase something in quote or code tags, you can use the relevant buttons on the advanced reply method or use

    [ c o d e ] [ / c o d e ]
    [ q u o t e ] [ / q u o t e ]
    (without the spaces inside the brackets)
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  6. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,972
    161
    189
    Hi there Fergal, hope youe easter is going well.

    Im struggling with how to implement some of the sample code you supplied

    Private Sub SetDirection(ByVal frog As Label)

    where would i actually put this as i know that if i get this right then the timer code will have no problem finding it etc , and than it should work etc???

    Thanks Eddie
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  7. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    you need to put the entire sub into the form class. Exactly where within that class you put it is entirely up to you, so long as its outside any other sub, but inside the class itself.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  8. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,972
    161
    189
    Im with you on that , for some reaon i was just looking for clarification.

    Anyway i think i may have to take the whole of the program to bits to see what is good and what is bad. as im getting stressed and confused so maybe a clean start will help me here ??

    Eddie
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  9. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    its certainly an option. scrub it all and start afresh.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  10. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,972
    161
    189
    Then thats what ill do.

    The bloke who has done the massive lines of code came out with the 'if its not broke dont fix it' line. He wouldnt admit that his code was too long and his excuse for not using 'case selects' is that hes not a fan of them ?? WTF

    Ill be able to use some of the code in there already so it shouldnt be that hard.

    To get the setdirection in there i could call a timer setdirection couldnt i , or would it be better to stick a label in somewhere and make sure its not seen on the main form ??

    Eddie
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  11. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,972
    161
    189
    Hey Fergal just so you know that your help has not gone in vein, heres the new program ive started. Using your example ive got the whole lot to move and the code is nowhere near as long.

    Granted i have to work out how to get them to move independantly, which is what ill be doing tonight and other little bits i need to do. But im on my way and its thanks to you

    BTW i dare say ill still need help though if thats ok with you

    Thanks
    Eddie
     

    Attached Files:

    Certifications: BSc computing and information systems
    WIP: 70-680

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.