shoot arrow

Blitz3D Forums/Blitz3D Beginners Area/shoot arrow

Rook Zimbabwe(Posted 2004) [#1]
So how does one shoot an arrow in a blitz game???
-RZ


Matty(Posted 2004) [#2]
It can be as simple or as complex as you like. The most basic method would be along the lines of:

When player presses fire button (keydown/mousedown etc)

if keydown(FireKey) then
NewArrow=CopyEntity(ArrowMesh)
positionentity NewArrow,entityx(firers entity),entityy(firers entity),entityz(firers entity)
rotateentity NewArrow,0,entityyaw(firers entity),0
endif

then each frame you would do this:
moveentity NewArrow,0,0,Speed#

That is very basic pseudocode for an extremely simple method of firing an arrow.


Rook Zimbabwe(Posted 2004) [#3]
I'll give it a shot, thanks!
_RZ


Rook Zimbabwe(Posted 2004) [#4]
OK aside from the PUN on giving this a shot... I got no joy. Let me post my code as it sits... Simple landscape... lonely castle... sunk boat (how you got here)... you and your xbow out to kick hiney and take names (except it is the age of mayhem and you didn't bring a pencil!)

I figure one of you bright boys can see what I done wrong... The program draws a new BOLT everyplac I move... kind of like pacman in reverse. No fire effect... nada!
-RZ


Matty(Posted 2004) [#5]
create a type:
type mybolt

field entityhandle

end type

then at the line "if mousehit(1)" then do this:

if mousehit(1) then
if animating(xbow)=0 then animate xbow,3
newbolt.mybolt=new mybolt
newbolt\entityhandle=copyentity(bolt)
positionentity newbolt\entityhandle,x,y,z
rotateentity newbolt\entityhandle,entitypitch(camera),entityyaw(camera),0
flushmouse
endif

then later in the code have the following:
Speed#=however fast you want them to move
for newbolt.mybolt=each mybolt
moveentity newbolt\entityhandle,0,0,Speed#
next


Rook Zimbabwe(Posted 2004) [#6]
I keep getting a variable type mismatch... HERE:
I am confused by the "." as well... I don't see the "." in the type examples in B3D. Type is a little confusing to me!
-RZ


Ross C(Posted 2004) [#7]
Your error comes from using:

newbolt=CopyEntity(bolt)


At the top of the code, then using it to reference type objects as well.

If Animating(xbow)=0 Then Animate xbow,3 
newbolt.mybolt=New mybolt


Change one of them to a different variable name. You can't have them both called new bolt :)


Your structure for the If's is incorrect in one part.

I'm confused by this line mainly:

If MouseDown (1) = True Then If MouseHit(1) Then 
If Animating(xbow)=0 Then Animate xbow,3 
newbolt.mybolt=New mybolt


Your checking to see if the mouse button has been pressed and held in basically. Best to do this:

If Mousehit (1) = True Then 
   If Animating(xbow)=0 Then Animate xbow,3 
   newbolt.mybolt=New mybolt
End if


See if you indent your code correctly, it helps greatly in tracking down errors. For example:

Graphics 800,600
SetBuffer BackBuffer()
x=100
y=100
While Not KeyHit(1)
Cls
If KeyHit(205) then
x=x+1
If x>700 then
x=x-1
End If
ElseIf KeyHit(203) then
x=x-1
If x<50 then
x=x+1
End If
End If

Rect x,y,5,5
Flip
Wend
End


Compared to:

Graphics 800,600
SetBuffer BackBuffer()

x=100
y=100

While Not KeyHit(1)
   Cls
   If KeyHit(205) then
      x=x+1
      If x>700 then
         x=x-1
      End If
   ElseIf KeyHit(203) then
      x=x-1
      If x<50 then
         x=x+1
      End If
   End If

   Rect x,y,5,5
   Flip
Wend
End


You can clearly see each If and what loop parts of code are in. Makes it easy to track down missing If's as well :)


Rook Zimbabwe(Posted 2004) [#8]
Yep... I started noticing the artifacts... Old code not cleaned up well (you should see the house!) : )

You caught some that were giving me trouble though... coludn't find them.

I do have one silly Q... Now that my Xbow sprays bolt like a firehose!

I tried to use entitytype to get the bolts to stop in a entitytype 2 object. 2 I am using for buildings. I just wanted to see if they would stop.

I don't see any bolts fire when I do this and I declare entitytype right after I load the bolt mesh.
-RZ


Rook Zimbabwe(Posted 2004) [#9]
Ross... I forgot to thank you for all you have done to teach me (and reteach me) Basic and good coding! : )
-RZ

I bet you even know why my animated B3D object (door) is still in the same place after it swings open!


Ross C(Posted 2004) [#10]
Well, you could use mousehit(1) instead of mousedown(1). That would mean that you need to click the mouse button to get it to fire.

Or, you could create a timer for it. A little example program to show the timer in action and setting a range for the bullets. Hold in the mouse button to shoot :)

(Also, tidy up your house!)

Graphics3D 800,600
SetBuffer BackBuffer()


Global cam=CreateCamera()
PositionEntity cam,0,0,-5

Global light=CreateLight()

Global gun=CreateCube()
ScaleEntity gun,0.3,0.3,3
PositionEntity gun,1,-1,0

Global main_bullet=CreateSphere(); create a bullter that the type objects can copy.
ScaleEntity main_bullet,0.3,0.3,0.3
HideEntity main_bullet

Type bullet
	Field entity
	Field speed#
	Field range
	Field pivot
End Type

Global gun_timer=MilliSecs() ; timer to track the difference in millisecs
Global gun_time=200; time in milliseconds that the gun will fire

While Not KeyHit(1)


	If MouseDown(1) And MilliSecs()>gun_time+gun_timer Then
		gun_timer=MilliSecs() ; reset the timer
		create_bullet(gun); call function to create a bullet. Pass across the gun entity to the function
						  ; so the bullet can come from the guns position
	End If
	

	update_bullet()
	UpdateWorld
	RenderWorld
	Flip
Wend
End

Function create_bullet(gun_entity)
	b.bullet=New bullet
	
		temp_x=EntityX(gun_entity,True)
		temp_y=EntityY(gun_entity,True)
		temp_z=EntityZ(gun_entity,True)
		
		b\entity=CopyEntity(main_bullet) ; copy the main bullet entity , into the type object
		b\pivot=CreatePivot()
		
		PositionEntity b\entity,temp_x,temp_y,temp_z
		RotateEntity b\entity,EntityPitch(gun_entity,True),EntityYaw(gun_entity,True),0
		PositionEntity b\pivot,temp_x,temp_y,temp_z
		
		b\range=40 ; the range of the bullets
		b\speed=1 ; the speed of the bullets
End Function

Function update_bullet()
	For b.bullet = Each bullet
		MoveEntity b\entity,0,0,b\speed
		If EntityDistance#(b\entity,b\pivot) > b\range Then
			FreeEntity b\entity
			FreeEntity b\pivot
			Delete b.bullet
		End If
	Next
End Function



Rook Zimbabwe(Posted 2004) [#11]
Thew weird thing is that if I use mousehit the xbow doesn't alowats fire... I click a lot of times for it to fire once. It may be my mouse but I think not.

I also tried to create a timer but I either STOP the game or it ignores the timer instance entirely!

-RZ


Rook Zimbabwe(Posted 2004) [#12]
GLOBAL... GLOBAL... OY!!!
-RZ


Ross C(Posted 2004) [#13]
;)