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. -
-
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[ilane]([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.
5 1719
I am creating a 'frogger' ... variables to specify the index in these cases works fine.
Okay, -
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).
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... - 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.
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... - 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(LaneNum, PlaceNum) = TheRealImageBox(I)
-
I = I + 1
-
Next
-
Next
From then on, you would always use the two-dimensional LanePlace() array to refer to them.
What do you think?
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?
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Marko Poutiainen |
last post by:
Situation:
We had to make our SQLServer 2000 database multi-lingual. That is, certain
things (such as product names) in the database should be shown in the
language the user is using (Finnish,...
|
by: altergothen |
last post by:
Hi there
I am a newbie to ASP.Net - Please Help!
I am trying to insert the values of my variables into a database.
If I try the following it works perfectly:
string insertQuery = "INSERT into...
|
by: 35th Ave Media |
last post by:
Hello,
I have about 60+ pages that I need to insert a MAILTO: tag so people can
email the page using their email client. The body of the message is
going to be the URL of that page. Using ASP,...
|
by: hfk0 |
last post by:
Hi,
I'm new to ASP.net, SQL Server and visual studio.net, and I'm having
problem inserting and storing data from a web form to a SQL database.
I created a simple ASP.NET web form, a simple SQL...
|
by: glenn |
last post by:
Hi folks,
The problem I have is that a query string works if hard-coded but
if I pass a variable to it, it does not work as shown here.
This works:
querystring="SELECT * FROM USERS WHERE...
|
by: pd123 |
last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error "
The name 'Peter' is...
|
by: tom c |
last post by:
I am going through "Walkthrough: Editing and Inserting Data in Web
Pages with the DetailsView Web Server Control" found at
http://msdn2.microsoft.com/en-us/library/sdba1d59.aspx
I am using...
|
by: kannaworld |
last post by:
Hai all
I wrote a common piece of code for inserting the records in a table using c#, i stored the control names in a Database table with the type of control, and the table field name will be...
|
by: dos360 |
last post by:
Hello,
I have two tables, one is a list of activities, the other a list of
participants. I want to insert one record in the activities table and
then using its identity column as foreign key, I...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |