Click to See Complete Forum and Search --> : Can't get my tank to shoot right
SteveCRM
Apr 27th, 2000, 05:25 AM
I can't figure this out:
Public Sub CheckKeys()
'SHOOTING
If KD(vbKeySpace) = True Then
Shoot
End If
End Sub
Public Sub Shoot()
Shot.y = Shot.y - 1
End Sub
When I hold down the space bar, the bullet goes; but when i lift it, the bullet stops. How do I keep the function going?
noone
Apr 27th, 2000, 07:13 AM
Do you have some kind of infinite loop that keeps running and updating everything? When the keyboard button is pressed you can set some variable to true. Your loop can check that variable in each iteration, and if its true, move the bullet along.
V(ery) Basic
Apr 28th, 2000, 01:22 AM
Or you can put replace the shoot thing with a timer and
enable it for shooting and disable it when it hits (if that
is what your game's about).
I know most people hate timers, but if you ask me they're a
mixed blessing.
Adios!
SteveCRM
Apr 28th, 2000, 04:56 AM
I don't exactly want to use a timer because Im using BitBlt. I used to use timers, yes most of the time they are blessings in disguise.
noone
Apr 28th, 2000, 08:08 AM
Steve did you get your tank shooting ok?
Well timers are useful when precise timing isnt important. However, when I was using them to control the main loop of my game it was pretty obvious that it wasnt going to do.
SteveCRM
Apr 28th, 2000, 08:24 AM
Hey noone, I only had a few minutes to check out your code. I checked vb world, and then my family and I set out for Connecticut (USA if you don't live there). On saturday I will be able to play with it some more. I'll tell you how It goes. Thanks!
kedaman
May 5th, 2000, 07:36 PM
I have an idea if you need to make multiple bullets to shoot, make a an UDT:
Type tBullet
x as integer
y as integer
speedx as integer
speedy as integer
end type
Dim bullet() as tbullet
And now you can have make it shot 75 bullets/second if you want, and the bullet goes in every direction you want
Use a mainloop in your game in the Sub Main with a doevents so that it can move the bullets if they're fired
noone
May 5th, 2000, 11:13 PM
Kedeman is right about the array of UDT, it's probably the best and easiest way to go.
kedaman
May 5th, 2000, 11:39 PM
Wait i got a better suggestion:
Make a classmodule instead of udt and make it handle the code for when it hits something or not. Then put a classcollection to add and remove new bullets, in this way you don't need to remove the last UDT in the array, Classcollection handles the referings and you don't need to rearange the udt everytime a bullet hits something before the last bullet
noone
May 6th, 2000, 01:39 AM
I thought about using classes too but was informed that it might be a bit too slow, and becuase VB isnt as OOP as Java or C++ wasnt worth it. What you might want to do is put a boolean variable in your UDT that determines if the bullet is currently active or not, if that variable is false you can just skip over it.
kedaman
May 6th, 2000, 02:22 AM
You may be right noone, that's if this tank game needs speed. You need to make a sub launched not as frequent as the main loop, to free up the already hit bullets from the UDT array.
noone
May 6th, 2000, 02:38 AM
Is there any reason to move them in and out of the array? I jsut always kept them in the array but set the active variable to false.
kedaman
May 6th, 2000, 02:54 AM
Well you will fill up the memory each time you fire a bullet, so they have to be removed from the udt as soon as a specified amount of bullets have been reached
noone
May 6th, 2000, 06:45 AM
About how much memory would each of those UDT take up?
kedaman
May 6th, 2000, 07:36 AM
Each UDT will take up as much as the vars inside it will take, go look at the datatype summary in the vb help
essohbee
May 6th, 2000, 09:47 PM
The use of a type is most possibly the best way to do it,
but maybe having instead of having .speedx and speedy you could use .angle in degrees since:
x = x + sin(angle / 57)
y = y + cos(angle / 57)
I think that perhaps you'll have to subtract 90 degress
from the angle, but testing will show if you need to do so in order to make certain that 0 is straight up.
The reason you divide in 57 is that you have to convert degress into radians, and 57 usable, although it's very aprox. If you want a closer value you can easily calculate
the value by doing 360 / (2 * pi).
Woopsie, this may be a little off the topic, but it's very usefull for a Shoot'm'up game.
kedaman
May 7th, 2000, 12:47 AM
Just learning how to use radians instead and put the angles in radians instead of degrees save both time and performance, ;). In this case it's better to put speedx and speedy and make the degree calculations from outside, or we end up with performance problems, if we have to calculate the new position with sin and cos every time.
noone
May 7th, 2000, 06:21 AM
I was just asking about taking them out of the array because I'm doing someting similar now and never take things out of the array. At initialization I create the array and put in as many as I'm going to use. Will this cause memory problems?
essohbee
May 7th, 2000, 08:47 AM
Of course, my mistake, wasn't thinking there.
Defining .xspeed and .yspeed as cos/sin values is
much better. And for even more increase of speed it's
wise to create cos and sin tables :)
kedaman
May 7th, 2000, 03:59 PM
Well I haven't calculated anything but you wont probably notice anything, as this UDT don't take much space but if you don't care for all your variable in you game, you might end up with a Ram eater
drewski
May 9th, 2000, 09:03 AM
Back to the arrays. How do you delete something from an array? I've heard of it but never have I seen it done.
kedaman
May 9th, 2000, 04:05 PM
Redim Preserve array(ubound(array)-1)
This will delete the last item in an array. So if you want to delete an item which is not the last, and the order doesn't matter, you can put the lastitem to the deleted items place and then redim it.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.