projectiles?

BlitzPlus Forums/BlitzPlus Beginners Area/projectiles?

Ice T.(Posted 2007) [#1]
hey everyone! Ice T. here, I need to learn how to make projectiles so that when a button is pressed my person shoots an arrow, or bullet, etc. also, I want it so that EVERY TIME I press the button a projetile is shot.

thanx


Matty(Posted 2007) [#2]
A simple way is to do the following:

create a 'type' like this:

type bulletobject

field x#,y#,z#      ;position in the world of the bullet
field angle#          ;direction bullet flies in
field speed#         ;overall speed of the bullet

field lifetime ;you may want your bullets to disappear after a while if they don't hit anything, or go off the screen

end type 


then when your creature/player presses the fire button you do something like this:


if keyhit(57)<>0 then ;space bar was pressed (fire button)

bullet.bulletobject=new bulletobject

bullet\x=;player position / gun muzzle position in the world
bullet\y=;as for bullet\x
bullet\z=;if you are doing a 2d game then this is not needed

bullet\speed=;what ever speed you want your bullets to fly at
bullet\angle=;whatever angle the player is facing in




endif 


okay once bullets are created we need to manage them and make sure they do what they are meant to do, so in your core loop / main loop you would have a call to the following function every frame:


;I assume your game code looks like this to some extent
repeat

;get player input, update player stuff...

UpdateBullets()

until keydown(1)<>0 or gameover

function UpdateBullets()

for Bullet.BulletObject=each BulletObject
Bullet\X=Bullet\X+Bullet\Speed*Cos(Bullet\Angle)
Bullet\Y=Bullet\Y+Bullet\Speed*Sin(Bullet\Angle)  



;check if the bullet hit anything here
;check if the bullet left the screen or should be removed and so so

next

end function