Problems with Functions

BlitzPlus Forums/BlitzPlus Programming/Problems with Functions

In this game I am making I have a death screen were the player can choose either to retry the game, or exit. The player chooses by pressing either one or two. When the player exits, the game calls on an exit function, which works perfectly. But when the player wants to retry, the program calls on the function that creates the level, but instead of working, the game crashes, what is going wrong?

This is the StartLevel() Function:
Function StartLevel()
While Not KeyHit(1) 	
	Cls
	DrawLevel()
	TestInput()
	TestEnemy()
	TestCollision()
	Timer()
	DrawHUD()
Flip
Wend
End Function



And here is the DeathScreen() Function:
 Function DeathScreen()
While Not KeyHit(1)
Cls
Delete Player 
Delete Computer
Delete Enemy
Delete Enemy2 
Text 260,240, "Game Over!"
Text 263,275, "Continue?"
Text 240,305, "  Yes"
Text 330,305, "No"

If KeyHit(2) Then
	
	StartLevel()	
		
ElseIf KeyHit(3) Then
	
	EndScreen()
	
End If

Flip
Wend
End Function


you deleted your players and your enemys and you didn't tell it run through a main loop so when it does the functions instructions it ends the program.

So how do i fix that?

first put your load player code in a function and put your main loop in a function and call load player and such in the function you call in the restart. the put run() [your main loop function] in at the end. heres an example

function run()
While Not KeyHit(1) 	
	Cls
	DrawLevel()
	TestInput()
	TestEnemy()
	TestCollision()
	Timer()
	DrawHUD()
Flip
Wend
end function
Function restart()
loadplayer()
loadenemy()
loadworld()
run()
endgame()
End Function


What do you mean by load player?

its a function that loads the player. example
function loadplayer()
p.player=new player
p\im=loadimage("image1.bmp")
p\x=400
p\y=300
p\otherstuff=more_other_stuff
end function


But I already did that as a global type

Never mind, i get what you are saying, Thank you very much.