|
-
Aug 17th, 2009, 07:23 PM
#1
Thread Starter
Member
Collision Detection
Hmm, I don't know where to start here. This is my code for a moving button:
If e.KeyCode = Keys.W Then
Button1.Top = Button1.Top - 10
ElseIf e.KeyCode = Keys.S Then
Button1.Top = Button1.Top + 10
ElseIf e.KeyCode = Keys.A Then
Button1.Left = Button1.Left - 10
ElseIf e.KeyCode = Keys.D Then
Button1.Left = Button1.Left + 10
How would I use the intersects/bounds thing a
nd where would I put it? Would it go here?
If e.KeyCode = Keys.W Then
--here
Button1.Top = Button1.Top - 10
People have told me the commands but I don't know how to use them...
Any help on collision detection would help :P
-
Aug 17th, 2009, 07:55 PM
#2
Re: Collision Detection
Look at .Bounds.IntersectsWith()
-
Aug 17th, 2009, 08:00 PM
#3
Thread Starter
Member
Re: Collision Detection
Actaully I found it before you posted but when I get there I get stuck because I made it so if it is intersecting with button3 then the keypress won't work. I don't know how to get around it though :/ Heres the code:
If e.KeyCode = Keys.W Then
If (Button1.Bounds.IntersectsWith(Button3.Bounds) = False) Then
Button1.Top = Button1.Top - 10
ElseIf e.KeyCode = Keys.S Then
If (Button1.Bounds.IntersectsWith(Button3.Bounds) = False) Then
Button1.Top = Button1.Top + 10
ElseIf e.KeyCode = Keys.A Then
If (Button1.Bounds.IntersectsWith(Button3.Bounds) = False) Then
Button1.Left = Button1.Left - 10
ElseIf e.KeyCode = Keys.D Then
If (Button1.Bounds.IntersectsWith(Button3.Bounds) = False) Then
Button1.Left = Button1.Left + 10
End If
End If
End If
End If
End If
-
Aug 17th, 2009, 08:06 PM
#4
Thread Starter
Member
Re: Collision Detection
Now I did this and it worked ok...but is there a better way?:
If (Button1.Bounds.IntersectsWith(Button3.Bounds) = True) Then
Button1.Top = Button1.Top + 1
Else
If e.KeyCode = Keys.W Then
Button1.Top = Button1.Top - 10
ElseIf e.KeyCode = Keys.S Then
Button1.Top = Button1.Top + 10
ElseIf e.KeyCode = Keys.A Then
Button1.Left = Button1.Left - 10
ElseIf e.KeyCode = Keys.D Then
Button1.Left = Button1.Left + 10
End If
End If
-
Aug 17th, 2009, 09:11 PM
#5
Re: Collision Detection
Well what if you move left into button3, then moving the control up one space won't do anything.
If you want button3 to be an object that stops you from moving then I store the location of Button1 (the piece you are moving) at the beginning of that code. Then at the end, check for a collision after it has been moved. If there is a collision, then restore Button1 back to the position it was in, keeping it from moving into button3.
Also you can clean up your move code by using a select case statement.
Code:
Select Case e.KeyCode
Case Keys.W
Button1.Top = Button1.Top - 10
etc...
End Case
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
|