Firing Code For VB


Are you having trouble getting a shooting code to work in VB? I've had the same problem many times, and I'll
help you! In the AI in VB tutorial, you'll see this code, but here it is simplified...
On to the lessons!



Standard Shooting

To do a standard shooting (Shows no bullets or flash), use this code:

Sub firemainleft()
If esub1.Left > yousub.Left - 5000 And esub1.Left < yousub.Left And esub1.Visible Then
esub1.Visible = False
ElseIf esub2.Left > yousub.Left - 5000 And esub2.Left < yousub.Left And esub2.Visible = True Then
esub2.Visible = False
ElseIf ship1.Left > yousub.Left - 5000 And ship1.Left < yousub.Left And ship1.Visible = True Then
ship1.Visible = False
ElseIf ship2.Left > yousub.Left - 5000 And ship2.Left < yousub.Left And ship2.Visible = True Then
ship2.Visible = False
ElseIf ship3.Left > yousub.Left - 5000 And ship3.Left < yousub.Left And ship3.Visible = True Then
ship3.Visible = False
End If
End Sub

Here's the explanation:
This code is to fire left only.You can modify it to your tastes for other directions.
Currently, the sub has a range of 5000 pixels (little dots on the screen). You can make its range longer of shorter by
increasing or decreasing the number of pixels. Just for reference, one inch is about 100 pixels.


Advanced Shooting

For this example, you'll need to save the following pictures to your hard drive:
and (You'll see the cannon better in your program).
Okay, now, use this code:

Sub firemainleft()
youtank.Picture = LoadPicture(app.Path & "tankshooting.gif")
tankswitchtimer.Enabled = True
If etank1.Left > youtank.Left - 5000 And etank1.Left < youtank.Left And etank1.Visible Then
etank1.Visible = False
ElseIf etank2.Left > youtank.Left - 5000 And etank2.Left < youtank.Left And etank2.Visible = True Then
etank2.Visible = False
ElseIf ship1.Left > yousub.Left - 5000 And ship1.Left < yousub.Left And ship1.Visible = True Then
ship1.Visible = False
ElseIf ship2.Left > yousub.Left - 5000 And ship2.Left < yousub.Left And ship2.Visible = True Then
ship2.Visible = False
ElseIf ship3.Left > yousub.Left - 5000 And ship3.Left < yousub.Left And ship3.Visible = True Then
ship3.Visible = False
End If
End Sub

You'll need to have the image boxes changed, needless to say, but you'll also need to create a timer. Here's the properties:

Name: tankswitchtimer
Enabled: False
Interval: 500

Here's another bit of code you'll need in the program:
Sub tankswitchtimer_Timer()
youtank.picture = LoadPicture(app.Path & "tankl.gif")
tankswitchtimer.Enabled = False
End Sub


This code will make it have a bright flash appear in front of the tank's cannon when it shoots!