Collision problem
Author |
Message |
Barda4
|
Posted: Fri Jan 18, 2008 12:51 pm Post subject: Collision problem |
|
|
Hi, I like many others are trying to finish my final program and my collisions are not working. If you can help me then please I would really appreate it!
code: | Dim Box1X1 As Integer = 150
Dim Box1Y1 As Integer = 100
Dim Box1X2 As Integer
Dim Box1Y2 As Integer
Dim Box2X1 As Integer = 200
Dim Box2Y1 As Integer = 200
Dim Box2X2 As Integer
Dim Box2Y2 As Integer
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Down Then
Box1Y1 += 2
PictureBox1.Location = New Point(Box1X1, Box1Y1)
End If
If e.KeyCode = Keys.Right Then
Box1X1 += 2
PictureBox1.Location = New Point(Box1X1, Box1Y1)
End If
If e.KeyCode = Keys.Left Then
Box1X1 -= 2
PictureBox1.Location = New Point(Box1X1, Box1Y1)
End If
If e.KeyCode = Keys.Up Then
Box1Y1 -= 2
PictureBox1.Location = New Point(Box1X1, Box1Y1)
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Box1X1 < Box2X2 And Box1X2 > Box2X1 And Box1Y1 > Box2Y2 And Box1Y2 < Box2Y1 Then
PictureBox2.Visible = False
End If
PictureBox2.Location = New Point(Box2X1, Box2Y1)
If Box1X1 > Box2X1 Then
Box2X1 += 2
End If
If Box1X1 < Box2X1 Then
Box2X1 -= 2
End If
If Box1Y1 > Box2Y1 Then
Box2Y1 += 2
End If
If Box1Y1 < Box2Y1 Then
Box2Y1 -= 2
End If
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
Box1X2 = Box1X1 + PictureBox2.Size.Width
Box1Y2 = Box1Y1 + PictureBox2.Size.Height
Box2X2 = Box2X1 + PictureBox1.Size.Width
Box2Y2 = Box2Y1 + PictureBox1.Size.Height
End Sub
End Class |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Megaryuu
|
Posted: Mon Jan 21, 2008 1:35 pm Post subject: Re: Collision problem |
|
|
I need more information - like what objects do you have on your window? |
|
|
|
|
|
Barda4
|
Posted: Mon Jan 21, 2008 1:36 pm Post subject: Re: Collision problem |
|
|
Here
Public Class Form2
#Region "Variables"
Dim X As Integer = 174
Dim Y As Integer = 157
Dim mover As Boolean = False
Dim mover2 As Boolean = False
Dim mover3 As Boolean = False
Dim mover4 As Boolean = False
Dim i As Integer
Dim X2 As Integer = 0
Dim Y2 As Integer = 0
Dim Box1X1 As Integer
Dim Box1Y1 As Integer
Dim Box1X2 As Integer
Dim Box1Y2 As Integer
Dim Box2X1 As Integer
Dim Box2Y1 As Integer
Dim Box2X2 As Integer
Dim Box2Y2 As Integer
#End Region
Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Down And mover = False Then
Y += 2
PictureBox1.Location = New Point(X, Y)
PictureBox4.Location = New Point(X, Y)
Else
Y = Y
End If
If e.KeyCode = Keys.Right And mover = False Then
PictureBox1.Location = New Point(X, Y)
PictureBox4.Location = New Point(X, Y)
If PictureBox1.Location.X <= 587 Then
PictureBox1.Location = New Point(X, Y)
Else
X += 2
End If
End If
If e.KeyCode = Keys.Left And mover = False Then
PictureBox1.Location = New Point(X, Y)
PictureBox4.Location = New Point(X, Y)
If PictureBox1.Location.X <= 0 Then
PictureBox1.Location = New Point(X, Y)
Else
X -= 2
End If
Else
X = X
End If
If e.KeyCode = Keys.Up And mover = False Then
Y -= 2
PictureBox1.Location = New Point(X, Y)
PictureBox4.Location = New Point(X, Y)
Else
Y = Y
End If
If e.KeyCode = Keys.Space Then
PictureBox1.Visible = False
PictureBox4.Visible = True
mover = True
End If
End Sub
Private Sub Form2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Space Then
PictureBox1.Visible = True
PictureBox4.Visible = False
mover = False
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If X2 >= 0 And X2 < 200 And Y2 = 0 Then
X2 += 2
ElseIf X2 = 200 And Y2 < 200 Then
Y2 += 2
ElseIf X2 > 0 And X2 <= 200 And Y2 = 200 Then
X2 -= 2
ElseIf X2 = 0 And Y2 > 0 Then
Y2 -= 2
End If
If Box1X1 < Box2X2 And Box1X2 > Box2X1 And Box1Y1 < Box2Y2 And Box1Y2 > Box2Y1 Then
PictureBox2.Visible = False
End If
PictureBox2.Location = New Point(X2, Y2)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
PictureBox4.Visible = False
Box1X1 = PictureBox1.Location.X
Box1Y1 = PictureBox1.Location.Y
Box1X2 = Box1X1 + PictureBox1.Size.Width
Box1Y2 = Box1Y1 + PictureBox1.Size.Height
Box2X1 = PictureBox2.Location.X
Box2Y1 = PictureBox2.Location.Y
Box2X2 = Box2X1 + PictureBox2.Size.Width
Box2Y2 = Box2Y1 + PictureBox2.Size.Height
End Sub
End Class |
|
|
|
|
|
Megaryuu
|
Posted: Mon Jan 21, 2008 2:12 pm Post subject: Re: Collision problem |
|
|
Here, this might help - insert in Form2_KeyDown.
code: | Select Case e.KeyCode
Case Keys.Down
If mover = False And PictureBox1.Location.X <= Me.Height - 64 Then
Y += 2
PictureBox1.Location = New Point(X, Y)
PictureBox4.Location = New Point(X, Y)
End If
Case Keys.Right
If mover = False And PictureBox1.Location.X <= Me.Width - 24 Then
X += 2
PictureBox1.Location = New Point(X, Y)
PictureBox4.Location = New Point(X, Y)
End If
Case Keys.Left
If mover = False And PictureBox1.Location.X > 0 Then
X -= 2
PictureBox1.Location = New Point(X, Y)
PictureBox4.Location = New Point(X, Y)
End If
Case Keys.Up
If mover = False And PictureBox1.Location.Y > 0 Then
Y -= 2
PictureBox1.Location = New Point(X, Y)
PictureBox4.Location = New Point(X, Y)
End If
Case Keys.Space
PictureBox1.Visible = False
PictureBox4.Visible = True
mover = True
End Select
|
|
|
|
|
|
|
Barda4
|
Posted: Fri Jan 25, 2008 9:19 pm Post subject: Re: Collision problem |
|
|
This is for anyone else who needs to know collision detection:
code: |
If <PictureBox>.Bounds.IntersectsWith(<EnemyPictureBox>.Bounds) Then
MsgBox("DEAD")
End If
|
Now the only problem is that I don't know where to place it so that when I am not moving I die |
|
|
|
|
|
|
|