How can I detect if a picture touches a line on my form??
Printable View
How can I detect if a picture touches a line on my form??
check the Games Programming Formum on this site!
If the picture is dynamically resizing? If the line is moving? If the picture is moving?
You can always check against the positioning values of the objects (top, left, height, width) to determine if they are touching.
Here is a really quick example:
To get this to work, on an empty form, place a line (straight line) across the form along the bottom somewhere. Put an Image on the form with a small graphic (I got mine from the common/graphics folder in visual studio).Code:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37
Image1.Left = Image1.Left - Image1.Width
Case 38
Image1.Top = Image1.Top - Image1.Height
Case 39
Image1.Left = Image1.Left + Image1.Width
Case 40
Image1.Top = Image1.Top + Image1.Height
End Select
If Image1.Top + Image1.Height > Line1.Y1 Then
Image1.Top = Line1.Y1 - Image1.Height
End If
End Sub
You move the image around with your cursor keys. It cannot go past the line on the bottom of the form.
Keep in mind this is just a quick hack and doesn't take into account diagonal lines or if a line is above the image.
The properties you need to look for in the line are X1, X2, Y1, and Y2 which correspond to the beginning of the line (X1, Y1) and the end of the line (X2, Y2).
Hope this helps you out.
Thanx! I'll try it! :)
The code worked fine!
Is it possible to do the same, when I have a line that responds to keystrokes, which will jump to the center of the screen, if it touches the edges of the form??
Well, it depends on if it is a verticle line or a horizontal line ... but yeah. Just check the X1, X2 and Y1, Y2 and if they hit the ScaleWidth then have them equal Int(ScaleWidth / 2)...
For instance:
This effect makes a verticle line move left and right (only) and when it gets to the edge of the form, jump to the middle of the form. This looks at both the left and right of the form.Code:Select Case KeyCode
Case 37
Line1.X1 = Line1.X1 - 100
Line1.X2 = Line1.X2 - 100
Case 39
Line1.X1 = Line1.X1 + 100
Line1.X2 = Line1.X2 + 100
End Select
If Line1.X1 >= Me.ScaleWidth Or Line1.X1 <= 0 Then
Line1.X1 = Int(Me.ScaleWidth / 2)
Line1.X2 = Line1.X1
End If
Forgot to add, if you want the line to go up and down the screen, check against ScaleHeight and Y1, Y2 ... :)
[Edited by ExcalibursZone on 11-03-2000 at 03:32 PM]
Hi again!
I'm making a game where, a line from the top of the screen falls down on a line that lies down in the bottom of the screen which have to catch the dropping line. When the bottom line catched the top line, the top line must go to the top again.
I have used this code to determine when the lines meet, but even if the two lines don't even meet, the top line goes to the top whenever it gets near the bottom line......
Could you tell me, what I am doing wrong??
Top line: Line2
Bottom line : Line1
Code:Private Sub Timer1_Timer()
Line2.Y1 = Line2.Y1 + 120
Line2.Y2 = Line2.Y2 + 120
If Line1.Y1 = Line2.Y1 Then
Hs = Hs + 1
Score.Caption = "Score: " & Hs
Timer1.Interval = 0
Timer1.Enabled = False
Line2.X1 = 4800
Line2.X2 = 4800
Line2.Y1 = 1200
Line2.Y2 = 1080
Timer1.Interval = 95
Timer1.Enabled = True
End If
End Sub
[Edited by CyberCarsten on 11-04-2000 at 09:22 AM]
change the line:
ToQuote:
Code:If Line1.Y1 = Line2.Y1 Then
This will test to make sure that if for some reason line 2 goes beyond line 1, it will trigger properly, of course, if your intent was to show a hit or a miss (miss = beyond) then you test for greater than after this statement, in the same if/then sequence.Code:If Line2.Y1 >= Line1.Y1 Then
That didn't work.....
What did you mean by this:
PS:Quote:
This will test to make sure that if for some reason line 2 goes beyond line 1, it will trigger properly, of course, if your intent was to show a hit or a miss (miss = beyond) then you test for greater than after this statement, in the same if/then sequence.
This is the code for the game:
[Edited by CyberCarsten on 11-04-2000 at 11:51 AM]Code:Dim Hs
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyRight: GoRight
Case vbKeyLeft: GoLeft
Case vbKeyEscape: End
End Select
End Sub
Sub GoRight()
Line1.X1 = Line1.X1 + 120
Line1.X2 = Line1.X2 + 120
End Sub
Sub GoLeft()
Line1.X1 = Line1.X1 - 120
Line1.X2 = Line1.X2 - 120
End Sub
Private Sub Form_Load()
Dim Hs As Integer
Hs = 0
End Sub
Private Sub Timer1_Timer()
Line2.Y1 = Line2.Y1 + 120
Line2.Y2 = Line2.Y2 + 120
If Line2.Y1 >= Line1.Y1 Then
Hs = Hs + 1
Score.Caption = "Score: " & Hs
Timer1.Interval = 0
Timer1.Enabled = False
Line2.X1 = 4800
Line2.X2 = 4800
Line2.Y1 = 1200
Line2.Y2 = 1080
Timer1.Interval = 65
Timer1.Enabled = True
End If
End Sub
Private Sub TitleTim1_Timer()
Title.ForeColor = &HFFFF&
TitleTim1.Enabled = False
TitleTim2.Enabled = True
End Sub
Private Sub TitleTim2_Timer()
Title.ForeColor = &HFF&
TitleTim1.Enabled = True
TitleTim2.Enabled = False
End Sub
Ahhh, ok. I re-read your post ... sorry for the confusion.
To make sure that you catch the falling line:
This should allow you to check if they touch. it can be 1 twip overlap on either end, which would allow for the line to pass right by, or go back to the top of the form. With a little modification of this code, you can definately add more precision.Code:Private Sub Timer1_Timer()
Line2.Y1 = Line2.Y1 + 120
Line2.Y2 = Line2.Y2 + 120
If CheckCatch = True Then
Hs = Hs + 1
Score.Caption = "Score: " & Hs
Timer1.Interval = 0
Timer1.Enabled = False
Line2.X1 = 4800
Line2.X2 = 4800
Line2.Y1 = 1200
Line2.Y2 = 1080
Timer1.Interval = 65
Timer1.Enabled = True
End If
End Sub
Private Function CheckCatch() As Boolean
If Line2.Y1 >= Line1.Y1 Then
If Line2.Y1 > Line1.Y1 Then
CheckCatch = False
Exit Function
End If
If (Line2.X1 >= Line1.X1 And Line2.X1 <= Line1.X2) Or _
(Line2.X2 >= Line1.X1 And Line2.X2 <= Line1.X2) Then _
CheckCatch = True
End If
End Function
Yes!!! It worked!!! Thank you VERY much!!! :)
Only one question left..... :)
When the timer has looped 3 times, the dropping stays away for a few seconds, and the reapear, but then the HS variable, which holds the highscore is being reduced by 1...why is that.....here is the "new code":
Code:Dim Hs
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyRight: GoRight
Case vbKeyLeft: GoLeft
Case vbKeyEscape: End
End Select
End Sub
Sub GoRight()
Line1.X1 = Line1.X1 + 120
Line1.X2 = Line1.X2 + 120
End Sub
Sub GoLeft()
Line1.X1 = Line1.X1 - 120
Line1.X2 = Line1.X2 - 120
End Sub
Private Sub Timer1_Timer()
RndGenCoord = Int((12000 * Rnd) + 240)
Line2.Y1 = Line2.Y1 + 120
Line2.Y2 = Line2.Y2 + 120
If CheckCatch = True Then
Hs = Hs + 1
Score.Caption = "Score: " & Hs
Timer1.Interval = 0
Timer1.Enabled = False
Line2.X1 = Line1.X1 + RndGenCoord
Line2.X2 = Line1.X1 + RndGenCoord
If Line2.X1 >= Me.ScaleWidth Or Line2.X1 <= 0 Then
Line2.X2 = Int(Me.ScaleWidth / 2)
Line2.X2 = Line2.X1
End If
Line2.Y1 = 2280
Line2.Y2 = 2280
Timer1.Interval = 25
Timer1.Enabled = True
End If
If Line2.Y2 > Line1.Y1 Then
Hs = Hs - 1
Score.Caption = "Score: " & Hs
Timer1.Interval = 0
Timer1.Enabled = False
Line2.X1 = 4800
Line2.X2 = 4800
Line2.Y1 = 2280
Line2.Y2 = 2280
Timer1.Interval = 25
Timer1.Enabled = True
End If
End Sub
Private Function CheckCatch() As Boolean
If Line2.Y1 >= Line1.Y1 Then
If Line2.Y1 > Line1.Y1 Then
CheckCatch = False
Exit Function
End If
If (Line2.X1 >= Line1.X1 And Line2.X1 <= Line1.X2) Or _
(Line2.X2 >= Line1.X1 And Line2.X2 <= Line1.X2) Then _
CheckCatch = True
End If
End Function
Private Sub TitleTim1_Timer()
Title.ForeColor = &HFFFF&
TitleTim1.Enabled = False
TitleTim2.Enabled = True
End Sub
Private Sub TitleTim2_Timer()
Title.ForeColor = &HFF&
TitleTim1.Enabled = True
TitleTim2.Enabled = False
End Sub
[Edited by CyberCarsten on 11-04-2000 at 05:50 PM]