|
-
Nov 3rd, 2000, 01:54 PM
#1
Thread Starter
Frenzied Member
How can I detect if a picture touches a line on my form??
-
Nov 3rd, 2000, 02:12 PM
#2
Addicted Member
check the Games Programming Formum on this site!
-
Nov 3rd, 2000, 02:24 PM
#3
Fanatic Member
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:
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
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).
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.
-
Nov 3rd, 2000, 02:44 PM
#4
Thread Starter
Frenzied Member
Thanx! I'll try it!
-
Nov 3rd, 2000, 02:58 PM
#5
Thread Starter
Frenzied Member
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??
-
Nov 3rd, 2000, 03:12 PM
#6
Fanatic Member
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:
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
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.
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]
-
Nov 4th, 2000, 09:07 AM
#7
Thread Starter
Frenzied Member
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]
-
Nov 4th, 2000, 10:54 AM
#8
Fanatic Member
change the line:
Code:
If Line1.Y1 = Line2.Y1 Then
To
Code:
If Line2.Y1 >= Line1.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.
-
Nov 4th, 2000, 11:41 AM
#9
Thread Starter
Frenzied Member
That didn't work.....
What did you mean by this:
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.
PS:
This is the code for the game:
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
[Edited by CyberCarsten on 11-04-2000 at 11:51 AM]
-
Nov 4th, 2000, 12:47 PM
#10
Fanatic Member
Ahhh, ok. I re-read your post ... sorry for the confusion.
To make sure that you catch the falling line:
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
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.
-
Nov 4th, 2000, 05:01 PM
#11
Thread Starter
Frenzied Member
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]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|