Delay when fireing

Blitz3D Forums/Blitz3D Beginners Area/Delay when fireing

Nebula Productions(Posted 2004) [#1]
Hi all,

For my new space game it is necessary to be able to use Keydown() when fireing whitout creating a new shot every frame...
In general it's easy to realize this by a code like this:

firedelay# = firedelay# - time_var# (frame indepandet variable)

if keydown(57) and firedelay# <= 0
"fire"
firedelay# = 1
endif

But because I use functions this wont work because "firedelay#" is not saved until the next frame.

So I thought I could use an extra Type;field like shot\firedelay# instead of simply firedelay#

But now Blitz wants me to set For shot.shot = each shot
and the function "fire" wont work any more.

Function feuer_frei()
For s.schuss = Each schuss

If KeyDown(57) And s\fire_delay# <= 0
s\fire_delay# = 1
fire_shot()
PlaySound(feuer)
EndIf
move_shot()

Next
End Function


I hope you got what I mean and please excuse the awful german-english mixed code :-)


Valgar(Posted 2004) [#2]
Here's the function i have writed times ago for a simple game...it fire a bullet at a certain time-rate...
Function create_missile() 
	a.missile=New missile
	a\image=missile
	a\posx=MouseX() ;the x position of missile creation is the mouse coords(the same coords of my jet of course) 
	a\posy=a\posy+MouseY()-30 ;the missile creation start a bit up to the jet(for simulating the launch-bay hehe) 
	a\avanzamento=4 ;the speed of the missile 
	a\contatore=0 ;the starting value of the animation 
End Function 

Function play_missile() 
	For a.missile=Each missile  
		DrawImage a\image,a\posx,a\posy 
		a\posy=a\posy-a\avanzamento ;this move the missile up 
		a\contatore=a\contatore+a\avanzamento ;this start the counter of the missile course 
		For b.alieno=Each alieno
			If ImagesCollide(a\image,a\posx,a\posy,0,b\image,b\movx,b\movy,0) ;if the alien collide with the missile the energy go down of one unit
					b\energia=b\energia-1
					If b\energia=<0 Then ;Delete b
					crea_esplosione()
					Delete b
					EndIf
					Delete a ;if the missile collide with the alien the missile stop it's existance :P
					Exit ;exit loop if missile is dead. will cause error as they's no missile to check against the aliens
			EndIf
		Next
	Next 
End Function 

And here's some snippet of the main code to shoot at intervals while holding the mouse button!
DrawImage aereo,MouseX(),MouseY() 
	
	If MouseDown(1)=1 ;if i hit the mouse button i create a missile 
	
		If MilliSecs() > timer_proiettili1 + 150 ;i use a "slowdown"of 100 millisecs to each fired bullet
		timer_proiettili1=MilliSecs() 
			create_missile() 
		EndIf 
	EndIf
	
	play_missile() ;this start the missil anim


I hope it's clear....


Nebula Productions(Posted 2004) [#3]
Thanks, but your code only works if it's not in a function. Unfortunately I must use a function to keep the code "readeable" (so that the main loop consists of as less lines/words as possible).


big10p(Posted 2004) [#4]
Nebula, why don't you just make firedelay global? It will retain it's value between function calls, then. ;)


Nebula Productions(Posted 2004) [#5]
You're my hero big10p!

I tried it some time before with global but, made a mistake as I now kwon.


Valgar(Posted 2004) [#6]
Yes make firedelay global or add a type field in this manner "alien\delay_type=millisecs()".
Make a type variable that contain the millisecs variable.


Valgar(Posted 2004) [#7]
Like this:
Function esplosione()
	For c.esplosione = Each esplosione
		Select c\grandezza
			Case 1	;small
				If MilliSecs() > c\timer + 50 ;i use a slowdown of 50 millisecs
					c\timer = MilliSecs()
					c\frame = (c\frame+1) Mod 15	;i loop trough the 7 frames of animation:this is a smaller explosion
				EndIf

				If c\frame => 14
					Delete c
				Else
					DrawImage explosion1,c\posizX,c\posizY,c\frame
				EndIf
		End Select
	Next
End Function

As you see it's a function.


Rook Zimbabwe(Posted 2004) [#8]
I the tutorials section there is a topic called CHECK FOR COLLISION USING TYPES

here: http://www.blitzbasic.co.nz/Community/posts.php?topic=35127

This has a multiple bullet gun with adjustable speed and a way to check for collision with the missle and do something if the missle hits something.

The bullet code is from Ross and Ross C. Rest is me (what I didn't learn from B3D.

Use... Enjoy...

-ROOK

PS I am most of a Newbie as well
: )


Nebula Productions(Posted 2004) [#9]
Now I have another problem.
How is it possible to get the world-relatve coordinates of a vertex.
The spaceship in my game has 2 guns, one on each wing, but I'm unable to let it shoot out of these especially when the ship is rolled.

My first attemp was to do it using Entityx() + x but that does only work when the ship is not turned or rolled.

My next attemp used entityroll() and Tan to get the position, but that worked just in some cases/angles.

Finaly I tried it using Getsurface() and Entityx() + Vertexx() but that didn't work either.


Rob Farley(Posted 2004) [#10]
Your best bet is to attach a couple of children to the right bits.

2 ways of doing this.

1. Stick a bone where the gun port is, name it something like gun and then in your code do: gunport = findchild(ship,"gun")
Then you can entityx(gunport,true) to find the xcoord etc.

2. Create a sphere (later turn it into a pivot) just after loading your ship mesh. Parent it to this ship (entiyparent gunport,ship) and move it around until it's in the right place.
Once it's in the right place instead of using a sphere use a pivot.
And again, entityx(gunport,true) will find it's position.

You can use entitypitch, yaw and roll too, just make sure you used the ,true flag when finding these to have them translated into real world positions.

I would strongly recomend using the bone method as then nothing in your game is hard coded (apart from the bone names) so you can update your ship etc without having to arse around with co-ords of children.


Nebula Productions(Posted 2004) [#11]
By sticking a bone to the object you mean a bone that is normaly used for animating?

I added the bone (3dsmax) named it "leftgun" and searched it with Findchild but the programm crashes when it has to position the shot, sending "Entity does not exsist".


Rob Farley(Posted 2004) [#12]
No idea about 3dsmax as I don't have $2000 knocking around in my back pocket.
Bones are case sensitive though and I believe some exporters add spaces or truncate the names. You'll have to experiment. Milkshape works fine.


[edit] You'll have to load your mesh with loadanimmesh instead of loadmesh otherwise you won't get any bones loaded.

This bit of code will at least display any bones that are in your mesh.
Graphics3D 640,480,32,2
mesh = LoadAnimMesh("ship.b3d")
c = CountChildren(mesh)
For n=0 To c
Print GetChild (mesh,n)
Next
WaitKey



Nebula Productions(Posted 2004) [#13]
Thanks, now it works!
Now I just must make the model having its texture again.

By the way its $4000, but there LEGAL ways to get it much cheaper.


Rob Farley(Posted 2004) [#14]
Either way, at that price I'd rather buy a new car.


Nebula Productions(Posted 2004) [#15]
I got a serious problem with the texture now, it won't show it.
If load the mesh with loadmesh, the bones are shown, the ship has its texture, but as you said Blitz doesn't recognize the bones.

If I load it with Loadanimmesh, it has its bones and recognizes them, but the texture is not shown.

The code is this (how do I make this nice black-green code window?):


ship\entity = LoadAnimMesh("models/ships/shipB.3ds")
PositionEntity ship\entity,0,-6,0
ScaleEntity ship\entity,0.1,0.1,0.1

EntityShininess ship\entity, 0.5
EntityType ship\entity, 1
EntityRadius ship\entity, 8

colortex = LoadTexture("models/ships/ship_texture.png")
TextureBlend colortex, 1
EntityTexture ship\entity, colortex,0,0