Inserting variable values into control names

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sedecrem
    New Member
    • Aug 2007
    • 4

    #1

    Inserting variable values into control names

    I am creating a 'frogger' like game. Essentially there are ten lanes each with an image array associated with it. The arrays are named from imglane0() to imglane9(). I was creating a piece of code that moved the img boxes in the array by random amounts whilst checking for collisions etc. and it worked, for one lane. I designed a module that would theoretically allow the movement of every lane without having to constantly repeat lines of code. The form passes on the lane number, the width of the images associated with that array and the number of controls within the array into a public sub within the module. the module uses these variables to do the above mentioned moving etc. in the specified lane. The problem is this, i am able to use a variable in place of an index in a control array name however i do not know how to use variables in a similar way to dynamically specify a control name without using needless amounts of select case statements.

    Code:
    Public Sub movement(ilane as integer, icount as integer, iwidth as integer)
    
    'ilane = lane number, icount = number of controls in array, iwidth = width of control
    
    For i = 1 To icount
    'i 1 is effectively just the index number of the img box that is behind img box i
        i1 = i
            If i1 < 0 Then
                i1 = icount
            End If
    'check for a collision between the images, what it does is irrelevant, only the syntax is troublesome
        If imglane[B][ilane][/B]([i1]).Left >= imglane[ilane]([i]).Left + iwidth Then
            imglane[ilane]([i]).Left = imglane[ilane]([i]).Left - 30
        End If
        imglane[ilane]([i]).Left = imglane[ilane]([i]).Left - Rnd * 10 + 1
    Next i
    For i = 0 To icount
        If imglane0([i]).Left <= 720 Then
        imglane0([i]).Left = 8280
    
        End If
    Next i
    End Sub
    That which is in bold causes me trouble, expects then or goto. essentially i need the syntax so that if ilane = 1 then the left property of control imglane1([i1]) would be used. using the variables to specify the index in these cases works fine.
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by Sedecrem
    I am creating a 'frogger' ... variables to specify the index in these cases works fine.

    Okay,
    [CODE=vb]
    If imglane[ilane]([i1]).Left >= imglane[ilane]([i]).Left + iwidth Then[/CODE]


    Can you post, what error it throwing. or just explain it in just 3 or 4 points.
    (Unable to get such a long sentence).
    Last edited by Killer42; Aug 20 '07, 09:49 AM. Reason: Shortened quote block

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Hm... interesting question.

      A reference using the name in a string like this is quite simple, using the Controls collection. However, I don't know how you'd go about using that in conjunction with a control array.

      For example...[CODE=vb]Debug.Print Me.Controls("im glane1").Left[/CODE]This syntax can be used to refer to a single (non-array) control. We might have to play around with this, and see what we can come up with.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Of course, one way to get around it would be to assign them as one big array, and simulate a two-dimensional array.

        In fact, you could even create a two-dimensional array in code and set the elements to reference the controls in the 1D array on the form. Seems as though a 2D array would be more convenient to work with - you could just refer to it as imgPosition(Lan eNumber, PositionInLane).

        For example, let's say you had three lanes with ten possible positions in each. You can define them all as one big control array at design time (or runtime) then in your code, at startup, you could do something like this...

        [CODE=vb]Dim LaneNum As Long, PlaceNum As Long
        ReDim LanePlace (1 To maxLanes, 1 To maxPlaces) As ImageBox
        Dim I As Long
        For LaneNum = 1 To maxLanes
        For PlaceNum = 1 To maxPlaces
        Set LanePlace(LaneN um, PlaceNum) = TheRealImageBox (I)
        I = I + 1
        Next
        Next[/CODE]
        From then on, you would always use the two-dimensional LanePlace() array to refer to them.

        What do you think?

        Comment

        • Sedecrem
          New Member
          • Aug 2007
          • 4

          #5
          Is the purpose of the code you posted to create and then populate the array based upon the original image box so that you can just use a loop on the giant array rather than worrying about individual 1d arrays?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by Sedecrem
            Is the purpose of the code you posted to create and then populate the array based upon the original image box so that you can just use a loop on the giant array rather than worrying about individual 1d arrays?
            My code was written with the assumption that you had already created the imagebox controls as a control array at design time. It just sets up a 2D array, each element of which is given a reference to one of the "real" controls.

            You're correct, the purpose of this is so that you can "just use a loop on the giant array rather than worrying about individual 1d arrays". I believe a 2D array, while requiring a little work to set up at program startup, will make your code much simpler.

            If you like, you could create just a single control at design time (set it's Index property to 0) then Load the rest at runtime. Is that what you had in mind?

            Comment

            Working...