Problems with array of shapes and mouse clicks
Author Message
Problems with array of shapes and mouse clicks
I am using VB6 pro and i have created and array (named wall) of equally
sized squares using the shape control. I have made quite a lot of them: 2914
i have made it into a big square that is 54 squares wide by 53 ssquares
long. I have created cose for the MouseDown event to find which square is
being clicked on (lots and lots of pictures) the problem is the code only
works for the top left as it goes to the right or down every six squares it
gets off by one square. I know this may sound confusing but here is the code
i have tried to comment as much as i could. basically i have two variables
one w for width in a for w = 0 to 54 and then one embedded h = 0 to 53 then
i test it with X,Y and if it works i convert it to i (the number of the
square in the array wall)
well here is my code:
Private Sub Form_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
w = 0 ' variable for row
h = 0 'variable for column
Dim i 'variable for index number
For h = 0 To 53 Step 1 'height 0 to 53
For w = 0 To 54 Step 1 'width 0 to 54
If CInt(X) >= (w * wall(0).Width) + wall(0).Left And CInt(X)
<= (w + 1) * wall_(0).Width + wall(0).Left Then 'test for in between x's
If CInt(Y) >= h * wall(0).Height + wall(0).Top And
CInt(Y) <= (h + 1) * wall_(0).Height + wall(0).Top Then 'test for in between
y's
'Following if's get the index number of the
corresponding square
If w >= 0 And w <= 6 Then i = (54 + 1) * h + w
If w >= 7 And w <= 13 Then i = (54 + 1) * h + w + 1
If w >= 14 And w <= 20 Then i = (54 + 1) * h + w + 2
If w >= 21 And w <= 27 Then i = (54 + 1) * h + w + 3
If w >= 28 And w <= 34 Then i = (54 + 1) * h + w + 4
If w >= 35 And w <= 41 Then i = (54 + 1) * h + w + 5
If w >= 42 And w <= 48 Then i = (54 + 1) * h + w + 6
If w >= 49 And w <= 55 Then i = (54 + 1) * h + w + 7

'Debug.Print i, "  ", w
Debug.Print X, Y
wall(i).BackStyle = 1 'make the square being clicked on
black
End If
End If
'Debug.Print test1, "  ", test2
Next w

Next h
'Debug.Print X, Y
End Sub

Andrew

Tue, 11 May 2004 02:29:26 GMT
Problems with array of shapes and mouse clicks
budda:

Howdy.  You don't need to use nested loops to find your clicked "cell."

First of all, some of your statements are contradictory so I want to clear them
up to make sure I don't give you the wrong answer.  You state that you arrange
the shapes into a 54x53 grid, but your nested loop loops from 0 to 53 and from 0
to 54: this is a 54x55 grid.  I will assume that the 54x55 grid is correct.
Next, you use a one-dimensional array -- presumably Wall(0 to 2969) -- to
represent your 54*55 grid.  This is not a problem, but it means you have to
perform a second calculation to convert your grid coordinates into a
one-dimensioanl offset within your one-dimensional array.  The formula for this
is:

' Z0 is the zero-based one-dimensional offset
' X0 is the zero-based two-dimensional X coordinate
' Y0 is the zero-based two-dimensional Y coordinate
' W1 is the one-based X extent - the width
' H1 is the one-based Y extent - the height
Z0 = X0 mod W1 + Y0 \ H1

This assumes that your mapping is in row-major order - i.e. you traverse the
cells horizontally and then move down to the next row.  If your mapping is
column-major order, then reverse the formula like this:

Z0 = X0 \ W1 + Y0 mod H1

But first you must calculate X0 and Y0 from the X and Y passed into your
MouseDown event.
Begin by correcting for the offset of your grid:

X0 = X - Wall(0).Left
Y0 = Y - Wall(0).Top

Now, you only need to scale X0 and Y0:

X0 = X0 \ Wall(0).Width
Y0 = Y0 \ Wall(0).Height

The "creeping" error you reported may be due to the fact that the shapes overlap
or that there may be a gap between your shapes.  You can correct for this -
assuming that the gap or the overlap is consistent- by using a better width and
height:

' note that this again assumes row-major order
WallWidth = Wall(1).Left - Wall(0).Left
WallHeight = Wall(55).Top - Wall(0).Top
X0 = X0 \ WallWidth
Y0 = Y0 \ WallHeight

So, the end result is:

WallWidth = Wall(1).Left - Wall(0).Left
WallHeight = Wall(55).Top - Wall(0).Top
X0 = X0 \ WallWidth
Y0 = Y0 \ WallHeight
Z0 = X0 mod W1 + Y0 \ H1
' Wall(Z0) was clicked

Hope this helps,
chris judge

Quote:

> I am using VB6 pro and i have created and array (named wall) of equally
> sized squares using the shape control. I have made quite a lot of them: 2914
> i have made it into a big square that is 54 squares wide by 53 ssquares
> long. I have created cose for the MouseDown event to find which square is
> being clicked on (lots and lots of pictures) the problem is the code only
> works for the top left as it goes to the right or down every six squares it
> gets off by one square. I know this may sound confusing but here is the code
> i have tried to comment as much as i could. basically i have two variables
> one w for width in a for w = 0 to 54 and then one embedded h = 0 to 53 then
> i test it with X,Y and if it works i convert it to i (the number of the
> square in the array wall)
> well here is my code:
> Private Sub Form_MouseDown(Button As Integer, _
>       Shift As Integer, X As Single, Y As Single)
>    w = 0 ' variable for row
>    h = 0 'variable for column
>    Dim i 'variable for index number
>    For h = 0 To 53 Step 1 'height 0 to 53
>             For w = 0 To 54 Step 1 'width 0 to 54
>                 If CInt(X) >= (w * wall(0).Width) + wall(0).Left And CInt(X)
> <= (w + 1) * wall_(0).Width + wall(0).Left Then 'test for in between x's
>                     If CInt(Y) >= h * wall(0).Height + wall(0).Top And
> CInt(Y) <= (h + 1) * wall_(0).Height + wall(0).Top Then 'test for in between
> y's
>                     'Following if's get the index number of the
> corresponding square
>                     If w >= 0 And w <= 6 Then i = (54 + 1) * h + w
>                     If w >= 7 And w <= 13 Then i = (54 + 1) * h + w + 1
>                     If w >= 14 And w <= 20 Then i = (54 + 1) * h + w + 2
>                     If w >= 21 And w <= 27 Then i = (54 + 1) * h + w + 3
>                     If w >= 28 And w <= 34 Then i = (54 + 1) * h + w + 4
>                     If w >= 35 And w <= 41 Then i = (54 + 1) * h + w + 5
>                     If w >= 42 And w <= 48 Then i = (54 + 1) * h + w + 6
>                     If w >= 49 And w <= 55 Then i = (54 + 1) * h + w + 7

>                     'Debug.Print i, "  ", w
>                     Debug.Print X, Y
>                     wall(i).BackStyle = 1 'make the square being clicked on
> black
>                     End If
>             End If
>             'Debug.Print test1, "  ", test2
>             Next w

>     Next h
>    'Debug.Print X, Y
> End Sub

> Andrew

Tue, 11 May 2004 04:19:56 GMT
Problems with array of shapes and mouse clicks

Dim shMat( 53, 54 ) As Shape

Q = 0
For L9 = 0 To 53
For L8 = 0 To 54
Q = Q + 1
Set shMat( L9, L8 ) = shShapeCtlArray( Q )
Next
Next

Now all you need to do is work out a way of cross referencing from an
Index to :  shMat( X, Y )

You could make the Tag tell you - or have an array of UDTs that do so

Quote:

>I am using VB6 pro and i have created and array (named wall) of equally
>sized squares using the shape control. I have made quite a lot of them: 2914
>i have made it into a big square that is 54 squares wide by 53 ssquares
>long. I have created cose for the MouseDown event to find which square is
>being clicked on (lots and lots of pictures) the problem is the code only
>works for the top left as it goes to the right or down every six squares it
>gets off by one square. I know this may sound confusing but here is the code
>i have tried to comment as much as i could. basically i have two variables
>one w for width in a for w = 0 to 54 and then one embedded h = 0 to 53 then
>i test it with X,Y and if it works i convert it to i (the number of the
>square in the array wall)
>well here is my code:
>Private Sub Form_MouseDown(Button As Integer, _
>      Shift As Integer, X As Single, Y As Single)
>   w = 0 ' variable for row
>   h = 0 'variable for column
>   Dim i 'variable for index number
>   For h = 0 To 53 Step 1 'height 0 to 53
>            For w = 0 To 54 Step 1 'width 0 to 54
>                If CInt(X) >= (w * wall(0).Width) + wall(0).Left And CInt(X)
><= (w + 1) * wall_(0).Width + wall(0).Left Then 'test for in between x's
>                    If CInt(Y) >= h * wall(0).Height + wall(0).Top And
>CInt(Y) <= (h + 1) * wall_(0).Height + wall(0).Top Then 'test for in between
>y's
>                    'Following if's get the index number of the
>corresponding square
>                    If w >= 0 And w <= 6 Then i = (54 + 1) * h + w
>                    If w >= 7 And w <= 13 Then i = (54 + 1) * h + w + 1
>                    If w >= 14 And w <= 20 Then i = (54 + 1) * h + w + 2
>                    If w >= 21 And w <= 27 Then i = (54 + 1) * h + w + 3
>                    If w >= 28 And w <= 34 Then i = (54 + 1) * h + w + 4
>                    If w >= 35 And w <= 41 Then i = (54 + 1) * h + w + 5
>                    If w >= 42 And w <= 48 Then i = (54 + 1) * h + w + 6
>                    If w >= 49 And w <= 55 Then i = (54 + 1) * h + w + 7

>                    'Debug.Print i, "  ", w
>                    Debug.Print X, Y
>                    wall(i).BackStyle = 1 'make the square being clicked on
>black
>                    End If
>            End If
>            'Debug.Print test1, "  ", test2
>            Next w

>    Next h
>   'Debug.Print X, Y
>End Sub

>Andrew

Tue, 11 May 2004 06:08:41 GMT
Problems with array of shapes and mouse clicks
Could you not use an array of picture boxes and simply use its MouseDown
event while checking the Index property?

Alex

Quote:

> I am using VB6 pro and i have created and array (named wall) of equally
> sized squares using the shape control. I have made quite a lot of them:
2914
> i have made it into a big square that is 54 squares wide by 53 ssquares
> long. I have created cose for the MouseDown event to find which square is
> being clicked on (lots and lots of pictures) the problem is the code only
> works for the top left as it goes to the right or down every six squares
it
> gets off by one square. I know this may sound confusing but here is the
code
> i have tried to comment as much as i could. basically i have two variables
> one w for width in a for w = 0 to 54 and then one embedded h = 0 to 53
then
> i test it with X,Y and if it works i convert it to i (the number of the
> square in the array wall)
> well here is my code:
> Private Sub Form_MouseDown(Button As Integer, _
>       Shift As Integer, X As Single, Y As Single)
>    w = 0 ' variable for row
>    h = 0 'variable for column
>    Dim i 'variable for index number
>    For h = 0 To 53 Step 1 'height 0 to 53
>             For w = 0 To 54 Step 1 'width 0 to 54
>                 If CInt(X) >= (w * wall(0).Width) + wall(0).Left And
CInt(X)
> <= (w + 1) * wall_(0).Width + wall(0).Left Then 'test for in between x's
>                     If CInt(Y) >= h * wall(0).Height + wall(0).Top And
> CInt(Y) <= (h + 1) * wall_(0).Height + wall(0).Top Then 'test for in
between
> y's
>                     'Following if's get the index number of the
> corresponding square
>                     If w >= 0 And w <= 6 Then i = (54 + 1) * h + w
>                     If w >= 7 And w <= 13 Then i = (54 + 1) * h + w + 1
>                     If w >= 14 And w <= 20 Then i = (54 + 1) * h + w + 2
>                     If w >= 21 And w <= 27 Then i = (54 + 1) * h + w + 3
>                     If w >= 28 And w <= 34 Then i = (54 + 1) * h + w + 4
>                     If w >= 35 And w <= 41 Then i = (54 + 1) * h + w + 5
>                     If w >= 42 And w <= 48 Then i = (54 + 1) * h + w + 6
>                     If w >= 49 And w <= 55 Then i = (54 + 1) * h + w + 7

>                     'Debug.Print i, "  ", w
>                     Debug.Print X, Y
>                     wall(i).BackStyle = 1 'make the square being clicked
on
> black
>                     End If
>             End If
>             'Debug.Print test1, "  ", test2
>             Next w

>     Next h
>    'Debug.Print X, Y
> End Sub

> Andrew

Tue, 11 May 2004 06:51:00 GMT
Problems with array of shapes and mouse clicks
Drat - I'd forgotton that a Shape has no events - however the 2D
Matrix approach is still valid. - I think.

On Thu, 22 Nov 2001 22:51:00 -0000, "Alex"

Quote:

>Could you not use an array of picture boxes and simply use its MouseDown
>event while checking the Index property?

>Alex

>> I am using VB6 pro and i have created and array (named wall) of equally
>> sized squares using the shape control. I have made quite a lot of them:
>2914
>> i have made it into a big square that is 54 squares wide by 53 ssquares
>> long. I have created cose for the MouseDown event to find which square is
>> being clicked on (lots and lots of pictures) the problem is the code only
>> works for the top left as it goes to the right or down every six squares
>it
>> gets off by one square. I know this may sound confusing but here is the
>code
>> i have tried to comment as much as i could. basically i have two variables
>> one w for width in a for w = 0 to 54 and then one embedded h = 0 to 53
>then
>> i test it with X,Y and if it works i convert it to i (the number of the
>> square in the array wall)
>> well here is my code:
>> Private Sub Form_MouseDown(Button As Integer, _
>>       Shift As Integer, X As Single, Y As Single)
>>    w = 0 ' variable for row
>>    h = 0 'variable for column
>>    Dim i 'variable for index number
>>    For h = 0 To 53 Step 1 'height 0 to 53
>>             For w = 0 To 54 Step 1 'width 0 to 54
>>                 If CInt(X) >= (w * wall(0).Width) + wall(0).Left And
>CInt(X)
>> <= (w + 1) * wall_(0).Width + wall(0).Left Then 'test for in between x's
>>                     If CInt(Y) >= h * wall(0).Height + wall(0).Top And
>> CInt(Y) <= (h + 1) * wall_(0).Height + wall(0).Top Then 'test for in
>between
>> y's
>>                     'Following if's get the index number of the
>> corresponding square
>>                     If w >= 0 And w <= 6 Then i = (54 + 1) * h + w
>>                     If w >= 7 And w <= 13 Then i = (54 + 1) * h + w + 1
>>                     If w >= 14 And w <= 20 Then i = (54 + 1) * h + w + 2
>>                     If w >= 21 And w <= 27 Then i = (54 + 1) * h + w + 3
>>                     If w >= 28 And w <= 34 Then i = (54 + 1) * h + w + 4
>>                     If w >= 35 And w <= 41 Then i = (54 + 1) * h + w + 5
>>                     If w >= 42 And w <= 48 Then i = (54 + 1) * h + w + 6
>>                     If w >= 49 And w <= 55 Then i = (54 + 1) * h + w + 7

>>                     'Debug.Print i, "  ", w
>>                     Debug.Print X, Y
>>                     wall(i).BackStyle = 1 'make the square being clicked
>on
>> black
>>                     End If
>>             End If
>>             'Debug.Print test1, "  ", test2
>>             Next w

>>     Next h
>>    'Debug.Print X, Y
>> End Sub

>> Andrew

Tue, 11 May 2004 19:17:41 GMT
Problems with array of shapes and mouse clicks

Thank you all for your help...it has been a great help,
Budda

Quote:

> I am using VB6 pro and i have created and array (named wall) of equally
> sized squares using the shape control. I have made quite a lot of them:
2914
> i have made it into a big square that is 54 squares wide by 53 ssquares
> long. I have created cose for the MouseDown event to find which square is
> being clicked on (lots and lots of pictures) the problem is the code only
> works for the top left as it goes to the right or down every six squares
it
> gets off by one square. I know this may sound confusing but here is the
code
> i have tried to comment as much as i could. basically i have two variables
> one w for width in a for w = 0 to 54 and then one embedded h = 0 to 53
then
> i test it with X,Y and if it works i convert it to i (the number of the
> square in the array wall)
> well here is my code:
> Private Sub Form_MouseDown(Button As Integer, _
>       Shift As Integer, X As Single, Y As Single)
>    w = 0 ' variable for row
>    h = 0 'variable for column
>    Dim i 'variable for index number
>    For h = 0 To 53 Step 1 'height 0 to 53
>             For w = 0 To 54 Step 1 'width 0 to 54
>                 If CInt(X) >= (w * wall(0).Width) + wall(0).Left And
CInt(X)
> <= (w + 1) * wall_(0).Width + wall(0).Left Then 'test for in between x's
>                     If CInt(Y) >= h * wall(0).Height + wall(0).Top And
> CInt(Y) <= (h + 1) * wall_(0).Height + wall(0).Top Then 'test for in
between
> y's
>                     'Following if's get the index number of the
> corresponding square
>                     If w >= 0 And w <= 6 Then i = (54 + 1) * h + w
>                     If w >= 7 And w <= 13 Then i = (54 + 1) * h + w + 1
>                     If w >= 14 And w <= 20 Then i = (54 + 1) * h + w + 2
>                     If w >= 21 And w <= 27 Then i = (54 + 1) * h + w + 3
>                     If w >= 28 And w <= 34 Then i = (54 + 1) * h + w + 4
>                     If w >= 35 And w <= 41 Then i = (54 + 1) * h + w + 5
>                     If w >= 42 And w <= 48 Then i = (54 + 1) * h + w + 6
>                     If w >= 49 And w <= 55 Then i = (54 + 1) * h + w + 7

>                     'Debug.Print i, "  ", w
>                     Debug.Print X, Y
>                     wall(i).BackStyle = 1 'make the square being clicked
on
> black
>                     End If
>             End If
>             'Debug.Print test1, "  ", test2
>             Next w

>     Next h
>    'Debug.Print X, Y
> End Sub