468,119 Members | 2,060 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Sub movement(ilane as integer, icount as integer, iwidth as integer)
  3.  
  4. 'ilane = lane number, icount = number of controls in array, iwidth = width of control
  5.  
  6. For i = 1 To icount
  7. 'i 1 is effectively just the index number of the img box that is behind img box i
  8.     i1 = i
  9.         If i1 < 0 Then
  10.             i1 = icount
  11.         End If
  12. 'check for a collision between the images, what it does is irrelevant, only the syntax is troublesome
  13.     If imglane[ilane]([i1]).Left >= imglane[ilane]([i]).Left + iwidth Then
  14.         imglane[ilane]([i]).Left = imglane[ilane]([i]).Left - 30
  15.     End If
  16.     imglane[ilane]([i]).Left = imglane[ilane]([i]).Left - Rnd * 10 + 1
  17. Next i
  18. For i = 0 To icount
  19.     If imglane0([i]).Left <= 720 Then
  20.     imglane0([i]).Left = 8280
  21.  
  22.     End If
  23. Next i
  24. End Sub
  25.  
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.
Aug 20 '07 #1
5 1398
hariharanmca
1,977 1GB
I am creating a 'frogger' ... variables to specify the index in these cases works fine.

Okay,
Expand|Select|Wrap|Line Numbers
  1. If imglane[ilane]([i1]).Left >= imglane[ilane]([i]).Left + iwidth Then

Can you post, what error it throwing. or just explain it in just 3 or 4 points.
(Unable to get such a long sentence).
Aug 20 '07 #2
Killer42
8,434 Expert 8TB
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...
Expand|Select|Wrap|Line Numbers
  1. Debug.Print Me.Controls("imglane1").Left
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.
Aug 20 '07 #3
Killer42
8,434 Expert 8TB
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(LaneNumber, 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...

Expand|Select|Wrap|Line Numbers
  1. Dim LaneNum As Long, PlaceNum As Long
  2. ReDim LanePlace (1 To maxLanes, 1 To maxPlaces) As ImageBox
  3. Dim I As Long
  4. For LaneNum = 1 To maxLanes
  5.   For PlaceNum = 1 To maxPlaces
  6.     Set LanePlace(LaneNum, PlaceNum) = TheRealImageBox(I)
  7.     I = I + 1
  8.   Next
  9. Next
From then on, you would always use the two-dimensional LanePlace() array to refer to them.

What do you think?
Aug 20 '07 #4
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?
Aug 21 '07 #5
Killer42
8,434 Expert 8TB
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?
Aug 21 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Marko Poutiainen | last post: by
2 posts views Thread by altergothen | last post: by
6 posts views Thread by 35th Ave Media | last post: by
5 posts views Thread by kannaworld | last post: by
5 posts views Thread by dos360 | last post: by
14 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.