PDA

Click to See Complete Forum and Search --> : complicated question involving object postions


spandex44
Jan 24th, 2000, 01:17 AM
Hi,
As the subject says, this is not an easy to explain question. I'll tell you a little bit about the program first:
I'm making a game where you are an army tank. When you press start, the map starts moving, giving you the effect that the tank is moving as well. By using the arrow keys, you can move the tank further up or down, or to the left or right. Using the space button, you can shoot a bullet. Because the user's tank and the enemy tank are not embedded into the map, I used labels that ARE embedded to make collision detection easier. So, what I did was put a label directly above the user's tank, and two label below the enemy tank.

Question:
I used the labels to detect collision. Likewise, I used them to determine whether or not a bullet has hit the enemy. When I run the game, if I stay still and shoot and the bullet hits the enemy, the collision is detected. Also, if I first move left or right, and then shoot, the collision is detected. In addition, if you move up or down and then shoot, the collision is detected.
However, if you move up/down and left/right before you shoot, the collision is not detected.

Sorry if this sounds a little bit confusing. I hope you understood. :)
If you need to see my code, please let me know.
Thanks.

------------------
Regards,
Alexander McAndrew
VB Zone
http://vbzone.cjb.net (http://gsenterprise.server101.com)

rino_2
Jan 24th, 2000, 01:22 AM
Hello spandex44,

Would it be possible for me to see your code?

Thanks

DiGiTaIErRoR
Jan 24th, 2000, 01:23 AM
confused me upload source

------------------
DiGiTaIErRoR

spandex44
Jan 24th, 2000, 01:53 AM
Ok, I thought it might be hard to understand. I took out all of the code to play sounds. As you will see, I'm not a very good programmer, so any help at all appreciated. Below is a list of the variables and picture/image files, and what they are used for:

i and n = counters for when I play sounds (not relevant)
BulletStartPos = a variable storing the starting value of a label's (a bullet in the game) TOP value
crashtrue = determines whether or not you crash when you drive into an enemy (helpful when the enemy is not visible because he has been destroyed)
picImage = An image box that moves to make the effect of the tank moving
picCar = the picture box containing the picture of the user's vehicle
picEnemy = the picture box containing the picture of the enemy's vehicle
Bullet = a label which looks like a bullet (in the game)
lblTop = a label directly above the user's vehicle. Used to generate the starting point of the bullet, and to detect collision with the enemy
lblEnemy = a label directly below the enemy. Used to detect collision
lblEnemyCrash = another label to detect collision (I used the Left value between the two labels)
BulletMove = a timer which makes a bullet move upwards
moveCar = a timer used to make the map move (which makes it look like the tank is moving)

The code:


Dim n, i
Dim BulletStartPos
Dim CrashTrue As String

Private Sub Command1_Click()
End
End Sub

Private Sub BulletMove_Timer()
Bullet.Top = Bullet.Top - 100
If Bullet.Top = BulletStartPos - 15000 Then
Bullet.Visible = False
BulletMove.Enabled = False
BulletStartPos = 0
End If

If Bullet.Left > lblEnemy.Left And Bullet.Left < lblEnemyCrash.Left And Bullet.Top = lblEnemy.Top Then
picEnemy.Visible = False
CrashTrue = False
End If
End Sub

Private Sub cmdMenu_Click()
Me.Hide
Form2.Show
End Sub

Private Sub cmdStart_Click()
MoveCar.Enabled = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight Then
picCar.Left = picCar.Left + 50
lblTop.Left = lblTop.Left + 50
End If

If KeyCode = vbKeyLeft Then
picCar.Left = picCar.Left - 50
lblTop.Left = lblTop.Left - 50
End If

If KeyCode = vbKeyUp Then
picCar.Top = picCar.Top - 50
lblTop.Top = lblTop.Top - 50
End If

If KeyCode = vbKeyDown Then
picCar.Top = picCar.Top + 50
lblTop.Top = lblTop.Top + 50
End If

If KeyCode = vbKeySpace Then
Bullet.Left = lblTop.Left
Bullet.Top = lblTop.Top
BulletStartPos = Bullet.Top
Bullet.Visible = True
BulletMove.Enabled = True
End If
End Sub


Private Sub Form_Load()
CrashTrue = True
End Sub

Private Sub MoveCar_Timer()
picImage.Top = picImage.Top + 50
picEnemy.Top = picEnemy.Top + 50
lblTop.Top = lblTop.Top - 50
If CrashTrue = True Then
If lblTop.Top = lblEnemy.Top And lblTop.Left < lblEnemyCrash.Left And lblTop.Left > lblEnemy.Left Then
MoveCar.Enabled = False
End If
End If
End Sub


I realize that this still probably isn't very clear. If you want to see the forms, or the whole project, please let me know.

Thanks a lot in advance.
Any help appreciated.


------------------
Regards,
Alexander McAndrew
VB Zone
http://vbzone.cjb.net (http://gsenterprise.server101.com)

spandex44
Jan 24th, 2000, 03:06 AM
Hi!
I was thinking about it for a while, and I firgured out what the problem is. The bullet always moves in intervals of 100 units (twips, or whatever is used to measure it). But if I move the vehicle, it moves in intervals of 50. Therfore, if I only move up once, the bullet will never have the same value for the TOP property. So to fix that, instead of using:

if bullet.top = lblenemy.top then
'do whatever
end if


I used:
if bullet.top < lblenemy.top then
'do whatever
end if

I also changed the value of CrashTrue to false, so that the loop is not activated whenever the bullet is above lblEnemy.

I don't expect anyone to understand any of this, but, I fixed the problem. Thanks if anyone was trying to figure it out.

------------------
Regards,
Alexander McAndrew
VB Zone
http://vbzone.cjb.net (http://gsenterprise.server101.com)