KeyHit Looping?

Blitz3D Forums/Blitz3D Beginners Area/KeyHit Looping?

Ace Killjoy(Posted 2008) [#1]
Not too long ago, I made a code for a simple explosion and put it in a game I've been working on.
It triggers like normal so far exept that in the original code I had to use KeyDown and hold the key down for the blast to expand.
When I trigger it in the game, it acts like I was using a KeyHit command and just shows the first part of the explosion as if it were the first frame.

I would like it to explode completely with a single hit of the key.
Here's the code for the Explosion:


I've tried several loops but they did not work and some even halted the program.
How can I make it explode with KeyHit?


Stevie G(Posted 2008) [#2]
This should work ...

;Simple Explosion
;----------------

Graphics3D 1024,768
SetBuffer BackBuffer()

light=CreateLight()
cam=CreateCamera()
PositionEntity cam,0,0,-30

Global cone=CreateCone()

Global flashfade#=1
Global boomx#=1
Global boomy#=1
Global boomfade#=0
Global boomsprite=LoadSprite("Blahblah.bmp")
HideEntity boomsprite

Global boom=LoadSound("Blah blah blah.wav")
Global boomchn

While Not KeyDown(1)

If boomfade = 0
	If KeyHit( 57 )
		create_explode()
	EndIf
Else
	update_explode()
EndIf

RenderWorld
Text 10,10,"Hit SPACE for a simple explosion"
Flip
Wend
End

;=============================================================
;=============================================================
;=============================================================

Function create_explode()

	If Not ChannelPlaying(boomchn)
		boomchn=PlaySound(boom)
		ChannelVolume boomchn,1
	EndIf

	ShowEntity boomsprite
	PositionEntity boomsprite,EntityX(cone,1),EntityY(cone,1),EntityZ(cone,1)
	
	boomx# = 1
	boomy# = 1
	boomfade# = 1

	ScaleSprite boomsprite,boomx#,boomy#
	
End Function

;=============================================================
;=============================================================
;=============================================================

Function update_explode()	
	
	boomx#=boomx#+.3
	boomy#=boomy#+.3
	ScaleSprite boomsprite,boomx#,boomy#
	
	boomfade#=boomfade#-.011
	
	If boomfade < 0 
		boomfade = 0
		HideEntity boomsprite
	Else
		EntityAlpha boomsprite,boomfade#
	EndIf
	
End Function




Ace Killjoy(Posted 2008) [#3]
Thanks, I'll try it out.
One thing I've always wondered about is what is the use of using updat functions? I've never figured it out.


Nate the Great(Posted 2008) [#4]
Ahhh you answered your own question there even though you didn't realize it

The point of update functions is to avoid having to deal with the question you originally posed.


Ace Killjoy(Posted 2008) [#5]
Ok... Now I'm confused. :P


Stevie G(Posted 2008) [#6]
With most events you do not want to halt the program flow in the main loop. So, you will need a function that kicks it off ( create_explode ) and one that keeps it going until the event is complete ( update_explode).

Hitting space starts the collision but only if the current explosion is complete. If the current explosion has started you cannot trigger another one until this is complete. The variable 'boomfade' acts like a timer here. When it's 0 no explosion is active so you can trigger another, then it's > 0 an explosion is in progress.

Clearly this code only works for a single explosion, I did not have time to change it to trigger a new explosion every time. I would recomment looking into types. You could also have a look at some particle code here :

http://www.blitzbasic.com/codearcs/codearcs.php?code=1095


Ace Killjoy(Posted 2008) [#7]
Cool. I'll take a look at it later.
I have been having some problems with a recycling bullet.

The code worked by the way.
I still need to figure out how to fix the sound effect problem, but all in good time.

Thank you!