KeyHit Looping?

Blitz3D Forums/Blitz3D Beginners Area/KeyHit Looping?

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:
;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#=1
Global boomsprite=LoadSprite("Blahblah.bmp")
HideEntity boomsprite

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

While Not KeyDown(1)

If MouseDown(1)=True Then ;<<<<<<<<And There it is. I want it to be KeyHit.
simple_explode()
If Not ChannelPlaying(boomchn)
boomchn=PlaySound(boom)
ChannelVolume boomchn,1
EndIf
EndIf

RenderWorld
Text 10,10,"Press and hold the left mouse button for a simple explosion"
Flip
Wend
End

Function simple_explode()

ShowEntity boomsprite
PositionEntity boomsprite,EntityX(cone,1),EntityY(cone,1),EntityZ(cone,1)

boomx#=boomx#+.3
boomy#=boomy#+.3
ScaleSprite boomsprite,boomx#,boomy#

boomfade#=boomfade#-.011
EntityAlpha boomsprite,boomfade#

End Function


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

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



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.

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.

Ok... Now I'm confused. :P

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

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!